Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading default application settings in C#

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?

like image 575
Ozgur Ozcitak Avatar asked Sep 08 '08 07:09

Ozgur Ozcitak


2 Answers

@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" 
like image 51
aku Avatar answered Oct 04 '22 10:10

aku


Reading "Windows 2.0 Forms Programming", I stumbled upon these 2 useful methods that may be of help in this context:

ApplicationSettingsBase.Reload

ApplicationSettingsBase.Reset

From MSDN:

Reload contrasts with Reset in that the former will load the last set of saved application settings values, whereas the latter will load the saved default values.

So the usage would be:

Properties.Settings.Default.Reset() Properties.Settings.Default.Reload() 
like image 23
Ohad Schneider Avatar answered Oct 04 '22 12:10

Ohad Schneider