1

I'm writing a console application in C# in Visual Studio Community 2019 for Mac, and I added an App.config file, I can't seem to access any of the values. How do I do this correctly? This may be related to the target framework being .Net core?

  • I added the System.Configuration.ConfigurationManager NuGet package.
  • I added the App config file according to here

The contents of the app config are very simple:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <appSettings>
       <add key="key" value="value" />
   </appSettings>
</configuration>

The program definition is also very simple:

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Config");
        Console.WriteLine(ConfigurationManager.AppSettings["key"]);
        foreach (var str in ConfigurationManager.AppSettings) {
            Console.WriteLine(str);
        }
        foreach (var str in ConfigurationManager.ConnectionStrings) {
            Console.WriteLine(str);
        }
        Console.WriteLine("End Config");
    }
}

When I run the project from the play button, I expect to see value twice in the output at least. Instead I get:

Config

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

End Config

Press any key to continue...

In the bin/debug folder there's only a ProjectName.dll and no ProjectName.config. What exactly could the problem be? Might it be related to this?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
mercifulhop
  • 271
  • 3
  • 11
  • 3
    the normal config in a .NET Core app is a json file, as far as I understand. Normally appSettings.json is the default. See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2 . So I expect you an put your extra value in there and read it quite easily within the app. Having said that, it looks like you _could_ use a .config XML file if you wanted, with a couple of extra steps: https://stackoverflow.com/questions/45034007/using-app-config-in-net-core – ADyson Apr 09 '19 at 09:49
  • 1
    Try checking AppDomain.CurrentDomain.SetupInformation.ConfigurationFile for where its reading/looking for that file – BugFinder Apr 09 '19 at 09:53

0 Answers0