User Settings in C#

Yes, accessing user settings in C# is easy.  It took me hours to realize that.

This MSDN article explains it all.  I advise you to slow down, read it, and follow the directions exactly – unlike what I did.

The key thing to know is:

–> In Solution Explorer, click the Properties folder and open up the Settings.settings file.  Type in a setting name:  for example, MySetting.  This creates a variable in the C# code that you can read – in this case, called Properties.Settings.Default.MySetting.

I made the mistake of right-clicking in Solution Explorer, selecting Add New, and adding a new Settings File.  This added a custom settings section to User.config, which I could then access in my code (after much experimentation) by using the variable SettingFileName.Default.MyProps

If you modify the setting, make sure to call Default.Save to write it back out to the config file.  You can only write user settings; application-scope settings are read-only.

Longer Discussion

Scope – You specify the setting as a “User” setting or an “Application” setting.  An Application setting is global for all users of the application.  It is read-only.  It is stored in the App.config file.  A User setting is unique for each user.  You can read or write user settings.  The default value is stored in App.config.  If you overwrite the default value, it will be saved in a User.config file buried in the Documents and Settings path.

Specifically, the User.config my program generated was located at:

C:\Documents and Settings\Elena\Application Data\SSEC\BuildIni\BuildIni.vshost.exe_Url_wxl4ze3exomgr1oy0bjneacclpsvjjyv\1.0.0.1

So, what does this path tell me?
– It’s under Documents and Settings\Elena, which means it is stored by user name.
– If the Roaming property is true, it is stored to Application Data.  If the Roaming property if false, it is stored under Local Settings\Application Data.
– SSEC is the company name defined for the assembly
– It is stored under BuildIni.vshost.exe_Url… because I was running from Visual Studio.  If I run BuildIni.exe directly, the file gets stored under BuildIni.exe_Url…
– Url_wxl… – Some kind of unique ID, but why and where does it come from?
– 1.0.0.0 – The assembly version.

It seems odd that the User.config location changes for the exe vs. vshost.exe, as well as changing for each assembly version.  I suppose this file is for very transient data like screen positions that may not be applicable across versions.

The article “Using My.Settings in VB 2005” does a great job of explaining some of the history and specifics of these config files.

Advertisements

4 Comments

  1. Trinkit
    Posted December 17, 2008 at 9:33 am | Permalink

    Thanks so much for this information, I found the xml files in my application directories, but, evidently those only provide ‘default’ settings if the user.config file isn’t found. It was bewildering me as to where my actual settings where being saved.

  2. Radu Crisan
    Posted January 23, 2010 at 12:38 pm | Permalink

    “Url_wxl… – Some kind of unique ID, but why and where does it come from?”

    I assume it is a kind of hash of the original path to the executable. Let’s say you copy your .exe file to another location. Question is now: how does the system know that he needs to create a separate settings file for the new copied application? Just try this and you’ll see different hash folders.

  3. Olaf
    Posted January 25, 2011 at 10:03 am | Permalink

    Url of “Using My.Settings in VB 2005” link has moved to http://msdn.microsoft.com/en-us/library/ms379611%28v=vs.80%29.aspx

    • braincrash
      Posted January 27, 2011 at 8:51 pm | Permalink

      Thanks!


Post a Comment

Required fields are marked *
*
*

%d bloggers like this: