Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Settings.Default.Save() not saving? [duplicate]

Tags:

c#

settings

Maybe you ran the same mistake as I did: setting the setting's scope to Application. Those kind of settings are not saved.

Set it to User to solve the problem.


rene is correct - you need to call Default.Reload after calling the Save method:

Settings.Default.Save();
Settings.Default.Reload();

Possibly a bug - ?

Posting as a reply to increase visibility -


After a full day of researching and studying the subject, I was able to solve this by putting the Configuration to the user:

Using System.Configuration; 

Properties.Settings.Default.strinconn = txt_stringconn.Text;
Properties.Settings.Default.Save ();
Properties.Settings.Default.Upgrade ();
MessageBox.Show ("Saved Settings");
Application.Restart ();

If your AssemblyInfo.cs file has a * in Assembly Version then it's refreshing the file every build so you won't see persistence or reliability until you change that to a hard number and rebuild all, then retest everything.


Using Visual Studio 2013 - there is nothing I could do to make it work reliably, I would call Save and it did not save. Save and then immediately Reload and it still would not retain the values on subsequent runs (probably related to when I stopped debugging could not identify the root cause) - very frustrating, likely there is an underlying bug but I cannot prove it.

To avoid getting crazy with this I decided to use the registry - the most fundamental way to keep app settings for an app. Recommended for you all. Here is the code:

public static class RegistrySettings
{

private static RegistryKey baseRegistryKey = Registry.CurrentUser;
private static string _SubKey = string.Empty;

public static string SubRoot 
{
    set
    { _SubKey = value; }
}

public static string Read(string KeyName, string DefaultValue)
{
    // Opening the registry key 
    RegistryKey rk = baseRegistryKey;

    // Open a subKey as read-only 
    RegistryKey sk1 = rk.OpenSubKey(_SubKey);

    // If the RegistrySubKey doesn't exist return default value
    if (sk1 == null)
    {
        return DefaultValue;
    }
    else
    {
        try
        {
            // If the RegistryKey exists I get its value 
            // or null is returned. 
            return (string)sk1.GetValue(KeyName);
        }
        catch (Exception e)
        {
            ShowErrorMessage(e, String.Format("Reading registry {0}", KeyName.ToUpper()));
            return null;
        }
    }
}

public static bool Write(string KeyName, object Value)
{
    try
    {
        // Setting
        RegistryKey rk = baseRegistryKey;
        // I have to use CreateSubKey 
        // (create or open it if already exits), 
        // 'cause OpenSubKey open a subKey as read-only
        RegistryKey sk1 = rk.CreateSubKey(_SubKey);
        // Save the value
        sk1.SetValue(KeyName, Value);

        return true;
    }
    catch (Exception e)
    {
        ShowErrorMessage(e, String.Format("Writing registry {0}", KeyName.ToUpper()));
        return false;
    }
}

private static void ShowErrorMessage(Exception e, string Title)
{
    if (ShowError == true)
        MessageBox.Show(e.Message,
                Title
                , MessageBoxButtons.OK
                , MessageBoxIcon.Error);
}
}

Usage:

private void LoadDefaults()
{
    RegistrySettings.SubRoot = "Software\\Company\\App";

    textBoxInputFile.Text = RegistrySettings.Read("InputFileName");
}

private void SaveDefaults()
{
    RegistrySettings.SubRoot = "Software\\Company\\App";

    RegistrySettings.Write("InputFileName", textBoxInputFile.Text);
}

Beware of calling

Settings.Default.Reload();

after each

Settings.Default.Save();

Save() function actually saves your changes to the file but it is not reflected to your running code. Thus, your code keeps the copy of the previous version of the file.

When you call Save() at another location in your code, it writes over your first change, effectively reverting your first change back to original value. Very hard to pin down even when debugging.