locked
My.Settings in C# RRS feed

  • Question

  •  

    What is the equivalent code in C# for the vb.net code "My.Settings"?

    I am not able to read the settings in C#.

    Thank you,
    Smith


    Smith
    Tuesday, July 29, 2008 9:26 PM

Answers

  • Here is the correct format of App.Config file if you want to access it through ConfigurationSettings.AppSettings["Key"] statement.

    For the given below App.Config you can access userSettings section through Properties.Settings.Default.LocalPath and  Properties.Settings.Default.FileCounter statement.
    But if you want to access it through appSettings section then you will need to use ConfigurationSettings.AppSettings["LocalPath"]  and ConfigurationSettings.AppSettings["FileCounter"]  statement

    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
        <configSections> 
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
                <section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> 
            </sectionGroup> 
        </configSections> 
        <userSettings> 
            <WindowsApplication1.Properties.Settings> 
                <setting name="LocalPath" serializeAs="String"
                    <value>C:\LocalPath</value> 
                </setting> 
                <setting name="FileCounter" serializeAs="String"
                    <value>100</value> 
                </setting> 
            </WindowsApplication1.Properties.Settings> 
        </userSettings> 
        <appSettings> 
            <add key="LocalPath" value="C:\LocalPath"/> 
            <add key="FileCounter" value="100"/> 
        </appSettings> 
    </configuration> 


    Pradeep Sethi
    Wednesday, July 30, 2008 8:52 AM
  • <applicationSettings> isn't actually a valid tag for a config file, but <appSettings> is.  Check out an article that lists a small (very limited) example of what an application configuration file should look like here:

    http://www.developer.com/net/net/article.php/3396111
    David Morton - Consultant - Catapult Systems - Houston
    Wednesday, July 30, 2008 2:28 PM
    Moderator

All replies

  • Configuration.AppSettings:

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

    One note: you'll need to reference System.Configuration to have access to this property.

    You can also access it by referencing System.Configuration and calling System.Configuration.ConfigurationManager.AppSettings["ConfigurationKeyGoesHere"]


    David Morton - Consultant - Catapult Systems - Houston
    Tuesday, July 29, 2008 9:29 PM
    Moderator
  • By Default Visual Studio creates the Settings file in Properties folder.

    It can be accessed through Properties.Settings.Default statement.

    For e.g If you have TestName,TestClass in the settings file then it can be accessed through following statements.

    Properties.Settings.Default.TestName
    Properties.Settings.Default.TestClass

    If you move Settings file out of Properties folder and keep it at root level then it can be accessed through following statements

    Settings.Default.TestName
    Settings.Default.TestClass


    Pradeep Sethi
    • Proposed as answer by EdSF1 Tuesday, April 3, 2012 8:56 PM
    Tuesday, July 29, 2008 9:42 PM
  • I am getting the Null value for the below statement.

    = ConfigurationSettings.AppSettings["LocalPath"];


    My app.config is as follows.

    <applicationSettings>

        <APPDownload.Properties.Settings>

            <setting name="LocalPath" serializeAs="String">

                <value>C:\LocalPath</value>

            </setting>

            <setting name="FileCounter" serializeAs="String">

                <value>100</value>

            </setting>

    </APPDownload.Properties.Settings>

    </applicationSettings>


    Smith
    Tuesday, July 29, 2008 10:50 PM
  • Here is the correct format of App.Config file if you want to access it through ConfigurationSettings.AppSettings["Key"] statement.

    For the given below App.Config you can access userSettings section through Properties.Settings.Default.LocalPath and  Properties.Settings.Default.FileCounter statement.
    But if you want to access it through appSettings section then you will need to use ConfigurationSettings.AppSettings["LocalPath"]  and ConfigurationSettings.AppSettings["FileCounter"]  statement

    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
        <configSections> 
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
                <section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> 
            </sectionGroup> 
        </configSections> 
        <userSettings> 
            <WindowsApplication1.Properties.Settings> 
                <setting name="LocalPath" serializeAs="String"
                    <value>C:\LocalPath</value> 
                </setting> 
                <setting name="FileCounter" serializeAs="String"
                    <value>100</value> 
                </setting> 
            </WindowsApplication1.Properties.Settings> 
        </userSettings> 
        <appSettings> 
            <add key="LocalPath" value="C:\LocalPath"/> 
            <add key="FileCounter" value="100"/> 
        </appSettings> 
    </configuration> 


    Pradeep Sethi
    Wednesday, July 30, 2008 8:52 AM
  • <applicationSettings> isn't actually a valid tag for a config file, but <appSettings> is.  Check out an article that lists a small (very limited) example of what an application configuration file should look like here:

    http://www.developer.com/net/net/article.php/3396111
    David Morton - Consultant - Catapult Systems - Houston
    Wednesday, July 30, 2008 2:28 PM
    Moderator
  • You have to access it directly.

    this.Backcolor = Properties.Settings.Default.myColor; // myColor is your app setting

    http://msdn.microsoft.com/en-us/library/bb397759.aspx
    Monday, March 28, 2011 2:49 PM