0
votes

I'm running Visual Studio 12.0 targeting 4.5. I'm running VS Express. My App.config looks like this:

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

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

    <applicationSettings>
        <ConfigMgrTest2.Properties.Settings>
            <setting name="exampleAppSetting" serializeAs="String">
                <value>example app setting data</value>
            </setting>
        </ConfigMgrTest2.Properties.Settings>
    </applicationSettings>
</configuration>

This allows me to use this syntax to access values:

string value = ConfigMgrTest2.Properties.Default.exampleAppSetting;

It seems from my research that I should have an "appSettings" section in my App.config that uses key-value pairs and looks like this:

<appSettings> 
    <add key="exampleAppSetting" value="example app setting data"/>    
</appSettings> 

This would allow me to access values like this:

string key = "exampleAppSetting";      
var appSettings = System.Configuration.ConfigurationManager.AppSettings;
string value = appSettings[key];

Using my App.config, any call to the ConfigurationManager.AppSettings property is obviously returning null.

The question is, Which version of App.config is "right"?

3

3 Answers

1
votes

I am not sure about which version is proper, which may depend on your actual usage scenario but if you have the posted section in your app.config like below

<appSettings> 
    <add key="exampleAppSetting" value="example app setting data"/>    
</appSettings> 

Then you need to access it like below. You need to mention the KEY for which you are trying to get the value.

ConfigurationManager.AppSettings["exampleAppSetting"]
1
votes

Assuming you have created a desktop application (WinForms or WPF) the App.config file is automatically copied to the output folder alongside with the executable and renamed to YourApplication.exe.config. It is inside this file that you could have the <appSettings> section:

<configuration>
    <appSettings> 
        <add key="exampleAppSetting" value="example app setting data"/>    
    </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

and then you will be able to access this value like this:

var appSettings = System.Configuration.ConfigurationManager.AppSettings;
string value = appSettings["exampleAppSetting"];

You might prefer using custom configuration sections when you have some complex config properties. In this case it might make more sense to move them in a separate configuration file and write a custom section.

1
votes

Both are right and current.

In your example,

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

You have simply used System.Configuration.ApplicationSettingsGroup to define a new configuration group applicationSettings.

if you were to add this to your config file:

<appSettings>
  <add key="Alfa" value="42"/>
</appSettings>

You could still retrieve it with:

var alfa = ConfigurationManager.AppSettings["Alfa"];

Update

Full App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
          <section name="ConfigMgrTest2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
      </configSections>

      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>

      <applicationSettings>
        <ConfigMgrTest2.Properties.Settings>
          <setting name="exampleAppSetting" serializeAs="String">
            <value>example app setting data</value>
          </setting>
        </ConfigMgrTest2.Properties.Settings>
      </applicationSettings>

      <appSettings>
        <add key="Alfa" value="42"/>
      </appSettings>

        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
    </configuration>

Personally, I prefer custom configurations, since they allow me to provide for default values, as well as make some mandatory.