Hope1_
Hope1_

Reputation: 25

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!

Upvotes: 1

Views: 217

Answers (1)

Caius Jard
Caius Jard

Reputation: 74720

  • 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!");

Upvotes: 1

Related Questions