Why are my application settings not getting persisted?
Asked Answered
U

6

22

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?

Underbelly answered 28/6, 2009 at 6:58 Comment(0)
D
26

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
Dicta answered 28/6, 2009 at 7:12 Comment(4)
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?Underbelly
XP will be C:\Documents and Settings\Ryan\Local Settings\Application DataMartimartial
I have read over 2 dozen answers and articles on this today, and this is the only one to get the name and location of the file correct. Of course, it's Jon Skeet.Bor
Great answer, save time.Salvia
M
21

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

Martimartial answered 28/6, 2009 at 7:25 Comment(3)
didn't know that there was a method called upgrade, neat.Witte
I used this approach here and had success: bytes.com/topic/c-sharp/answers/…Heatstroke
Awesome answer, and elegant NeedsUpgrading tip!Rentier
W
2

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.

Witte answered 28/6, 2009 at 7:17 Comment(0)
I
0

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.

Immunoreaction answered 23/11, 2022 at 16:41 Comment(0)
G
0

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);
        }
    }
Giulio answered 8/3 at 16:9 Comment(0)
T
-1

"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.

Thumbprint answered 11/1, 2021 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.