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>
This question is related to
c#
The reason is simple, your call to ConfigurationSettings.AppSettings
is not returning the required config file. Please try any of the following ways:
ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings["StartingMonthColumn"]
Hope this helps
Try to rebuild your project - It copies the content of App.config
to
"<YourProjectName.exe>.config" in the build library.
Try:
string value = ConfigurationManager.AppSettings[key];
For more details check: Reading Keys from App.Config
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.
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.
Also add the key "StartingMonthColumn" in App.config that you run application from, for example in the App.config of the test project.
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
.
Source: Stackoverflow.com