Sathish
Sathish

Reputation: 1631

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>

Upvotes: 137

Views: 398767

Answers (8)

Yar
Yar

Reputation: 7476

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

enter image description here

Upvotes: 23

James
James

Reputation: 82136

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.

Upvotes: 175

Hizabr
Hizabr

Reputation: 461

Also add the key "StartingMonthColumn" in App.config that you run application from, for example in the App.config of the test project.

Upvotes: 2

Asaf
Asaf

Reputation: 487

Try to rebuild your project - It copies the content of App.config to "<YourProjectName.exe>.config" in the build library.

Upvotes: 4

mugume david
mugume david

Reputation: 635

The reason is simple, your call to ConfigurationSettings.AppSettings is not returning the required config file. Please try any of the following ways:

  • Make sure your app config has the same name as your application's exe file - with the extension .config appended eg MyApp.exe.config
  • OR you can use ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings["StartingMonthColumn"]

Hope this helps

Upvotes: 8

JamesL
JamesL

Reputation: 221

Try:

string value = ConfigurationManager.AppSettings[key];

For more details check: Reading Keys from App.Config

Upvotes: 20

Bittercoder
Bittercoder

Reputation: 12143

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.

Upvotes: 21

Michael Burr
Michael Burr

Reputation: 340426

This:

Console.WriteLine( "StartingMonthColumn is {0}", ConfigurationManager.AppSettings["StartingMonthColumn"]);

works fine for me.

Note that ConfigurationManager is in the System.Configuration namespace (so you'll likely want a using System.Configuration; statement), and that since what you read in has a string type you'll need to parse what you read in to use it as a number.

Also, be sure you set system.configuration.dll as a reference in your project or build script.

Upvotes: 7

Related Questions