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?

JBurace
  • 5,123
  • 17
  • 51
  • 76

1 Answers1

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();
Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 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 Jan 08 '15 at 20:11
  • @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` – Scott Chamberlain Jan 08 '15 at 20:14
  • Does that mean the config saves to the publish dir, or the locally installed PC? – JBurace Jan 08 '15 at 20:26
  • 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. – Scott Chamberlain Jan 08 '15 at 20:33