Reading default application settings in C#

Ozgur Ozcitak picture Ozgur Ozcitak · Sep 8, 2008 · Viewed 43.6k times · Source

I have a number of application settings (in user scope) for my custom grid control. Most of them are color settings. I have a form where the user can customize these colors and I want to add a button for reverting to default color settings. How can I read the default settings?

For example:

  1. I have a user setting named CellBackgroundColor in Properties.Settings.
  2. At design time I set the value of CellBackgroundColor to Color.White using the IDE.
  3. User sets CellBackgroundColor to Color.Black in my program.
  4. I save the settings with Properties.Settings.Default.Save().
  5. User clicks on the Restore Default Colors button.

Now, Properties.Settings.Default.CellBackgroundColor returns Color.Black. How do I go back to Color.White?

Answer

aku picture aku · Sep 8, 2008

@ozgur,

Settings.Default.Properties["property"].DefaultValue // initial value from config file

Example:

string foo = Settings.Default.Foo; // Foo = "Foo" by default
Settings.Default.Foo = "Boo";
Settings.Default.Save();
string modifiedValue = Settings.Default.Foo; // modifiedValue = "Boo"
string originalValue = Settings.Default.Properties["Foo"].DefaultValue as string; // originalValue = "Foo"