Search code examples
c#wpfconfigurationmanager

WPF configurationmanager.appsettings collection is empty


I have a WPF application (mvvm) with multiple projects. In the main project I have an App.config file. I've added a couple of settings via Settings.settings. The App.config file looks like:

<configSections>
  <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    <section name="VSAutomation.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </sectionGroup>
<section name="log4net" type="log4net.config.Log4NetConfigurationSectionHandler, log4net" />

log4net stuff here...
 <applicationSettings>
    <VSAutomation.Properties.Settings>
        <setting name="SimDir" serializeAs="String">
            <value>"C:\Program Files (x86)\sim"</value>
        </setting>
    </VSAutomation.Properties.Settings>
</applicationSettings>

When I try to access these settings from one of the other modules

string appFolder = ConfigurationManager.AppSettings.Get("SimDir");

It returns null. If I set a breakpoint and evaluate

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

It points to the correct appname.exe.config file and the settings are correct.

ConfigurationManager.AppSettings.Count returns 0

This is really odd. I've looked at dozens of questions here StackOverflowflow but have not found a solution. Is it related to the log4net section?


Solution

  • Here is my solution, just in case anyone else has this specific version of the problem. I was trying to use the Settings.settings UI in Visual Studio. It created the structure shown above. After reviewing this article closely I tried creating an appSettings section and that worked.

    <appSettings>
       <add key="SimDir" value="C:\Program Files (x86)\sim"/>
    </appSettings>
    

    So now

    string appFolder = ConfigurationManager.AppSettings.Get("SimDir");
    

    retrieves the correct value.