Search code examples
c#visual-studiopropertiessaveapplication-settings

Why using string indexer on 'Settings.Default' to change a property value is not saved/persisted?


I have a .NET Framework app, and I need to save and load the state of bool variable. I tried to do this, using MyApp.Properties library. Here my code for save:

        private static void ChangeBoolState()
        {
            warningMessageState = false;
            Settings.Default["warningMessageState"] = warningMessageState;
            Settings.Default.Save();
        }

And here for load:

 warningMessageState = Convert.ToBoolean(Settings.Default["warningMessageState"]);

When I try to load or to save this variable using this code, I get the error:

System.Configuration.SettingsPropertyNotFoundException: "Settings property 'warningMessageState' could not be found."

Anybody know, what am I doing wrong? Thank You in advance!


Solution

    • On the project menu click properties
    • On the left click settings
    • Something like this appears (or maybe "this project doesn't contain a Settings file, click here to create one" - click there): enter image description here
    • Make sure that grid contains a User Scoped bool named WarningMessageState (use pascal case; it will end up as a class property)
    • Then you can use code like:
    Properties.Settings.Default.WarningMessageState = true;
    
    Properties.Settings.Default.Save();
    
    if(Properties.Settings.Default.WarningMessageState)
        MessageBox.Show("Warning!");