0

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?

utengr
  • 3,225
  • 3
  • 29
  • 68
cce1911
  • 363
  • 3
  • 20
  • https://stackoverflow.com/questions/2101273/how-do-i-retrieve-applicationsettings-from-a-loaded-app-config-file You should be able to access it by just typing `VSAutomation.Properties.Settings.SimDir` – Szabolcs Dézsi Oct 27 '17 at 23:55
  • I tried that but I get a compiler error saying VSAutomation doesn't exist in the current context. I'm trying to access these settings from another project (mvvm module) in the solution. I already have a reference from the startup project to this project, so I can't add a reference from this project to the startup. – cce1911 Oct 28 '17 at 02:08

1 Answers1

0

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.

cce1911
  • 363
  • 3
  • 20