Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading from app.config file

Tags:

c#

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>
like image 369
Sathish Avatar asked Oct 05 '22 13:10

Sathish


People also ask

How read app config file in C# Windows application?

Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.

What is app config file?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


1 Answers

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.

like image 175
James Avatar answered Oct 08 '22 02:10

James