137

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>
Doug Porter
  • 7,721
  • 4
  • 40
  • 55
Sathish
  • 1,631
  • 4
  • 13
  • 15
  • Are you trying to run the app from the \bin\debug folder while changing the config file at the root of the project? Otherwise, change the config and rebuild the code after running a cleanup. – Kangkan Mar 08 '10 at 08:45
  • 1
    The file isn't named "app.config" right? It's named that in your project for sure, but in the application output directory, it should be named the same thing as your output binary + ".config" (so like "MyLibrary.dll.config", or "MyProgram.exe.config", etc...) – BrainSlugs83 Sep 12 '13 at 05:40
  • @BrianSlugs83 thanks mate, I was wondering why my app crashes. Now, after two days of searching everywhere, the answer is here in the comment. I just had "app.config" and no "MyProgram.exe.config". – śmiglidigli Jul 02 '15 at 15:13
  • Please look at https://stackoverflow.com/questions/1189364/reading-settings-from-app-config-or-web-config-in-net for solution. Thanks – Ariful Islam Jun 20 '17 at 08:01

8 Answers8

175

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.

James
  • 80,725
  • 18
  • 167
  • 237
  • 2
    If ConfigurationSettings.AppSettings["StartingMonthColumn"] returns null or an empty string I would suspect that Int32.Parse will throw an exception on attempting to parse that values. – Bittercoder Mar 08 '10 at 10:10
  • @bittercoder, yes it would however, your app.config file should **always** have the correct configuration anyway. If the OP wants to add some form of handling in there i.e. *TryParse* then by all means they can. However, this wasn't the question. – James Mar 08 '10 at 10:39
  • 2
    Note that you can also get access to the strong typed versions of the values as well, via Settings.Default.* (i.e., in this case it would be Settings.Default.StartingMonthColumn). – BrainSlugs83 Sep 12 '13 at 05:43
  • @BrainSlugs83 Note that your getting confusing *user* settings with app settings. If you want to strongly-typed access to app settings you would need to implement a custom [ConfigurationSection](http://msdn.microsoft.com/en-us/library/2tw134k3.aspx) – James Sep 12 '13 at 07:50
  • That's not correct, it will pull values from app.config regardless of if the values are scoped to "User" or "Application" (from the GUI designer, you can choose the scope for each setting). – BrainSlugs83 Sep 18 '13 at 23:08
  • @BrainSlugs83 That's not what I was referring to - to put it simply, `Properties.Settings.Default` **does not** correspond to the `` configuration block - see [this answer](http://stackoverflow.com/questions/909688/what-is-the-difference-between-app-config-file-and-xyz-settings-file#909712) for an explanation of the difference. – James Sep 19 '13 at 07:59
  • That's true. It will read values out of the "applicationSettings" node, but not the "appSettings" node. -- I still contend that writing your own parsing logic for something that's built-in is overkill. – BrainSlugs83 Sep 20 '13 at 07:18
  • @BrainSlug83 Baring in mind that not *every* value will need parsed (only non-string values) I don't think it's that much of an issue. However, that's not to say you shouldn't use the `applicationSettings` - see the [Pros and cons of appSettings vs applicationSettings](http://stackoverflow.com/questions/460935/pros-and-cons-of-appsettings-vs-applicationsettings-net-app-config). The crux of it is `appSettings` is just easier if you are looking to add some quick configuration to your app. – James Sep 20 '13 at 07:46
  • @James I would say it easier to quickly add configuration settings using applicationSettings because there is the GUI and they are strongly typed so less typing. However they do not seem to work on mono: http://stackoverflow.com/questions/23594269/mono-not-reading-values-from-my-app-config – markmnl May 12 '14 at 06:42
23

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

enter image description here

Yar
  • 7,020
  • 11
  • 49
  • 69
21

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.

Bittercoder
  • 11,753
  • 10
  • 58
  • 76
  • Hi Bittercoder when i try to get the value for AppDomain.CurrentDomain.SetupInformation.ConfigurationFile i get C:\\Program Files\\Microsoft Office\\OFFICE11\\exCEL.EXE.config which is not my config file... what should i do – Sathish Mar 08 '10 at 08:58
  • That's the correct configuration file... it may just be that you weren't expecting it. In fact in this case I believe C:\Program Files\Microsoft Office\OFFICE11\Excel.EXE.config is the global config, and if you place a similiarly named Excel.EXE.config in the same directory as your assembly, this is where your app settings will be read from. See these links for details: http://www.dotnet247.com/247reference/msgs/56/281797.aspx http://stackoverflow.com/questions/2288575/can-i-use-access-the-app-config-from-net-code-when-called-via-com – Bittercoder Mar 08 '10 at 10:08
20

Try:

string value = ConfigurationManager.AppSettings[key];

For more details check: Reading Keys from App.Config

chown
  • 51,908
  • 16
  • 134
  • 170
JamesL
  • 221
  • 2
  • 2
8

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

mugume david
  • 635
  • 9
  • 18
7

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • what is the reference that should be added to get ConfigurationManager.Appsettings i use .Net 2.0 Framework – Sathish Mar 08 '10 at 08:53
  • 1
    You need a reference to `System.Configuration.dll`. The `ConfigurationManager` class was added in .NET 2.0, so you should be OK. – Michael Burr Mar 08 '10 at 09:23
4

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

asvignesh
  • 787
  • 2
  • 6
  • 32
Asaf
  • 477
  • 6
  • 6
2

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

Hizabr
  • 423
  • 5
  • 6