ryeguy
ryeguy

Reputation: 66851

Why are my application settings not getting persisted?

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?

Upvotes: 22

Views: 12415

Answers (7)

Nottoc
Nottoc

Reputation: 105

My situation when I ran into this problem is different than other answers. All my settings were user settings. However, I wanted the default values to be empty or null so I gave none of the properties a default value.

The save didn't work until I did give them a default value.

Upvotes: 0

Mario Favere
Mario Favere

Reputation: 510

I use a little config-file to solve this:

    private static void ReadSettings()
    {
        if (File.Exists("settings.cfg"))
            using (StreamReader sr = new StreamReader("settings.cfg"))
            {
                LaatsteSpecifiekeFoutMelding = sr.ReadLine();
                LaatsteAlgemeneFoutMelding = sr.ReadLine();
            }
    }
    private static void SaveSettings()
    {
        using (StreamWriter sw = new StreamWriter("settings.cfg",false))
        {
            sw.WriteLine(LaatsteSpecifiekeFoutMelding);
            sw.WriteLine(LaatsteAlgemeneFoutMelding);
        }
    }

Upvotes: 0

adrianwadey
adrianwadey

Reputation: 1759

I found that when running the Debug version it always rebuilds and auto increments the version. The new version expected the user.config file to be in C:\Users\Adrian\AppData\Local\Company\Project\Version. As this was a new version, the file didn't exist and I got the defaults again. You can check for this by changing a setting, saving, and then manually run the exe again (in the bin\debug\... folder). If this is your problem then it will have remembered your change.

To fix this you can use the Settings.Default.Upgrade() Method. The potential problem with this is that if you run it every time it will always replace your most recent settings with old ones.

SOLUTION Add a new setting using the settings editor:

SettingsHaveBeenUpgraded : bool : User : false

In Program.cs Main() add:

    if (!Settings.Default.SettingsHaveBeenUpgraded)
    {
        Settings.Default.Upgrade();
        Settings.Default.SettingsHaveBeenUpgraded = true;
    }

This will ensure the upgrade only happens once.

Upvotes: 0

David
David

Reputation: 1058

"User" defined settings are recorded in app.config along with "Application" defined settings. In reality however, "User" named settings is a misnomer and quite misleading. They should be called "Default User" settings.

When a new user accesses the application, they will be assigned the "User" setting by default.

User settings are only saved to the actual User's user.config settings file if they differ from the "Default User" setting located in the app.config file.

Upvotes: -1

csjohnst
csjohnst

Reputation: 1678

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

Upvotes: 21

Jon Skeet
Jon Skeet

Reputation: 1500504

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

Upvotes: 26

dr. evil
dr. evil

Reputation: 27265

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.

Upvotes: 2

Related Questions