Question

I am trying to use Settings.settings to define/persist some vars. For brevity, I've set up a test file to demonstrate the behavior I'm seeing:

First, I define a setting in Settings.settings:

From the Settings.settings window (variableName

I then have the following code to test changing variableName:

    public Form1()
    {
        InitializeComponent();

        string newString = Properties.Settings.Default.variableName;
        Properties.Settings.Default.variableName = "This is a new string";
        Properties.Settings.Default.Save();
    }

Running the above in the debugger for the first time, I grab the current value (the value I set in the Settings.settings window initially) of variableName from Properties.Settings. As expected, newString is set to "This is a string". Good.....

After executing the next two lines, the debugger shows variableName changed to "This is a new string". Good....

I then run the app through the debugger again. I hit the string newString line and, prior to execution, newString is undefined (of course). Good....

Undefined...not executed yet

As soon as I execute...

string newString = Properties.Settings.Default.variableName;

... and on subsequent executions of the code, the actual value of variableName is defined as "This is a new string" (Good...as expected).

I then go back to the Settings.settings window. variableName has not changed - it's still "This is a string". I've even closed VSE 2012 and re-opened the project. Settings.settings never changes.

Where is the new value being stored? I've checked all of the .config files ([appname].exe.config, [appname].vshost.exe.config, app.config, and the Settings.settings file) and the new value, "This is a new string" isn't anywhere to be found.

In summary, I'm getting the result I desire from the code, but I can't seem to view the result at design time other than to check the value of the var in the debugger. This seems not only peculiar to me, but impossible.

What am I missing/where am I not looking? I would fully expect the value of variableName to change in the Settings.settings window, but it never does. I've looked everywhere on StackOverflow/Google and can't seem to find the answer.

Thanks in advance....

Was it helpful?

Solution

The original value that you configured via Settings.settings is stored in a .config file alongside your executable's assembly. This will never change unless you modify the Settings file directly via Visual Studio; it's essentially a read-only file.

The user's customized setting is stored in a separate config file within the user's profile. The location of this file depends on your assembly's metadata. For example, on Windows 7/Vista the location might look like:

C:\Users\<user name>\AppData\Local\<company name>\<assembly name>\
   AssemblyName\<version>\user.config

If you haven't customized the company name in your assembly's metadata then it may default to Microsoft. Also note that AppData is a hidden folder that may not be visible in Windows Explorer depending on your view settings.

OTHER TIPS

I am not sure if I understand your question. That variable content stay persistent. Thats it. Why you would set a persistent variable to change it later?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top