0

I have an App.config file that contains this (generated by Visual Studio, no manual edits):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="FaxMonitorCSharp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <userSettings>
        <FaxMonitorCSharp.Properties.Settings>
            <setting name="BaseFolder" serializeAs="String">
                <value>C:\IncFaxesTest</value>
            </setting>
        </FaxMonitorCSharp.Properties.Settings>
    </userSettings>
</configuration>

How do I retrieve the value of BaseFolder in C#? I have tried:

string g_fax_loc = System.Configuration.ConfigurationSettings.AppSettings["BaseFolder"];

But I'm getting an obsolete method warning and instead asking me to use a very long method name with a ! in it.

Am I missing something?

5
  • I think the new way is ConfigurationManager.AppSettings["BaseFolder"]; it changed in 2.0 (which was a rather long time ago). Commented Jan 8, 2015 at 19:55
  • Does your current code retrieve the correct value? Commented Jan 8, 2015 at 19:55
  • 1
    Do you have that setting inside Settings class (going to your project properties and going to the settings tab would generate it) Commented Jan 8, 2015 at 19:56
  • @ScottChamberlain: has the only correct comment so far. The three answers are wrong. This is a case of the .NET strongly-typed settings feature. Commented Jan 8, 2015 at 19:58
  • I added the full file for reference. Commented Jan 8, 2015 at 20:01

1 Answer 1

4

The configuration file was generated automatically by VS after I used the "Properties" screen ...

If you generated it from the properties screen then you should use the strongly typed datatype visual studio generates for you when you use that screen instead.

string baseFolder = FaxMonitorCSharp.Properties.Settings.Default.BaseFolder;

or if you include using FaxMonitorCSharp.Properties; in your file you are going to use it you can just shorten it to

var baseFolder = Settings.Default.BaseFolder;

As a FYI, as I can see that the setting is in the userSettings section, if you want an assignment to get saved and reloaded the next time the program is opened you also need to call Save() on the settings class to make your changes written out to the hard drive.

Settings.Default.BaseFolder = "C:\Example";
Settings.Default.Save();
Sign up to request clarification or add additional context in comments.

4 Comments

No errors on this, seems to work. I don't think I plan on any changes to the value on-screen yet, but thanks!
@JBurace if you plan on not ever having the setting change and it is not user specific you may want to change it in the property menu to be a Application Setting instead of a User Setting that way the value of the setting will be read from the FaxMonitorCSharp.exe.config file in the same folder as your exe instead of the user.config file located in %LocalAppData%\YourCompanyName\FaxMonitorCSharp.exe_Url_SomeHash\1.0.0.0\user.config
Does that mean the config saves to the publish dir, or the locally installed PC?
The application config settings are wherever the executeable is located (with a backup copy hardcoded in to the program itself if the config file is not present). The defaults for the user settings is also stored there. The user setting file is on the user's local machine in their %LocalAppData% folder, if the file does not exist it creates it using the values from the app config file for the defaults.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.