views:

522

answers:

3

So I have some settings that are of the user scope. But for some reason, they are not being saved to the .exe.config file. I do the following:

    Properties.Settings.Default.Email="[email protected]";
    Properties.Settings.Default.Save();

Yet I look at the settings file in the debug folder and it is still the default that I set in visual studio. Am I doing this wrong?

+5  A: 

User settings are specific to the user, so they wouldn't get saved back to the .exe.config file, which is system wide.

From the docs of LocalSettingsProvider:

Application-scoped settings and the default user-scoped settings are stored in a file named application.exe.config, which is created in the same directory as the executable file. Application configuration settings are read-only. Specific user data is stored in a file named username.config, stored under the user's home directory.

So for a UserSettingsTest application just run from VS under the debugger (hence the vshost bit) I ended up with a path of:

C:\Users\Jon\AppData\Local\UserSettingsTest
  \UserSettingsTest.vshost.e_Url_pdqoppugkz1vaawbhwkkcu5ibxpi2fgu
  \1.0.0.0\user.config
Jon Skeet
I have WinXP, but I didn't see any such file or folder in C:\Documents and Settings\Ryan\Application Data. Is this the right place?
ryeguy
Um, not sure on XP. Will try in the morning.
Jon Skeet
XP will be C:\Documents and Settings\Ryan\Local Settings\Application Data
csjohnst
Downvoters - please comment.
Jon Skeet
A: 

All user scope settings saved under application data with within a folder which indicates the version of your application and the name.

You can see these folders by clicking "synchronize" in your "application settings" dialog.

In Vista generally:

  • c:\users[currentuser]\AppData\Local[CompanyName][AppName]\version
  • c:\users[currentuser]\AppData\Roaming[CompanyName][AppName]\version

Done this way due to settings are related with current user and UAC. In Vista also you can see even the application-wide settings are not stored in the config file.

[CompanyName] and [ProductName] comes from your Assembly Information settings.

dr. evil
+6  A: 

If you have your Assembly info set to automatically generate any version numbers (1.0.*), then every time you debug your app the version number will be different, and so will be creating a new file every time.

If this is the case you will need to perform an upgrade on the settings file:

Properties.Settings.Default.Upgrade()

You can also set a setting of NeedsUpgrading to true by default and set it to false after performing an upgrade so that end users who are not changing version numbers every time the app is started will not be Upgrading all the time

csjohnst
didn't know that there was a method called upgrade, neat.
dr. evil
I used this approach here and had success: http://bytes.com/topic/c-sharp/answers/522191-how-maintain-user-config-settings-when-upgrading
Jared Updike