reading from app.config file

I am trying to read StartingMonthColumn and CategoryHeadingColumn from the below app.config file using the code

ConfigurationSettings.AppSettings["StartingMonthColumn"]

but it is returning null, also ConfigurationSettings.AppSettings.Count returns zero

Please help me to read this in my windows application

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="CTARepository.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <CTARepository.Properties.Settings>
            <setting name="Setting" serializeAs="String">
                <value />
            </setting>
        </CTARepository.Properties.Settings>
    </userSettings>
    <appSettings>
        <add key="StartingMonthColumn" value="7"/>
        <add key="CategoryHeadingColumn" value="1"/>
    </appSettings>
</configuration>

Solution 1:

ConfigurationSettings.AppSettings is obsolete, you should use ConfigurationManager.AppSettings instead (you will need to add a reference to System.Configuration)

int value = Int32.Parse(ConfigurationManager.AppSettings["StartingMonthColumn"]);

If you still have problems reading in your app settings then check that your app.config file is named correctly. Specifically, it should be named according to the executing assembly i.e. MyApp.exe.config, and should reside in the same directory as MyApp.exe.

Solution 2:

Just for the future reference, you just need to add System.Configuration to your references library:

enter image description here

Solution 3:

ConfigurationSettings.AppSettings is deprecated, see here:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationsettings.appsettings.aspx

That said, it should still work.

Just a suggestion, but have you confirmed that your application configuration is the one your executable is using?

Try attaching a debugger and checking the following value:

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

And then opening the configuration file and verifying the section is there as you expected.