Reading default application settings in C#
Solution 1
@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"
Solution 2
Reading "Windows 2.0 Forms Programming", I stumbled upon these 2 useful methods that may be of help in this context:
ApplicationSettingsBase.Reload
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()
Solution 3
Im not really sure this is necessary, there must be a neater way, otherwise hope someone finds this useful;
public static class SettingsPropertyCollectionExtensions
{
public static T GetDefault<T>(this SettingsPropertyCollection me, string property)
{
string val_string = (string)Settings.Default.Properties[property].DefaultValue;
return (T)Convert.ChangeType(val_string, typeof(T));
}
}
usage;
var setting = Settings.Default.Properties.GetDefault<double>("MySetting");
Solution 4
Properties.Settings.Default.Reset()
will reset all settings to their original value.
Solution 5
I've got round this problem by having 2 sets of settings. I use the one that Visual Studio adds by default for the current settings, i.e. Properties.Settings.Default
. But I also add another settings file to my project "Project -> Add New Item -> General -> Settings File" and store the actual default values in there, i.e. Properties.DefaultSettings.Default
.
I then make sure that I never write to the Properties.DefaultSettings.Default
settings, just read from it. Changing everything back to the default values is then just a case of setting the current values back to the default values.
Ozgur Ozcitak
Updated on January 14, 2020Comments
-
Ozgur Ozcitak over 4 years
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:
- I have a user setting named
CellBackgroundColor
inProperties.Settings
. - At design time I set the value of
CellBackgroundColor
toColor.White
using the IDE. - User sets
CellBackgroundColor
toColor.Black
in my program. - I save the settings with
Properties.Settings.Default.Save()
. - User clicks on the
Restore Default Colors
button.
Now,
Properties.Settings.Default.CellBackgroundColor
returnsColor.Black
. How do I go back toColor.White
? - I have a user setting named
-
Ohad Schneider over 14 yearsI also prefer this approach, since calling {Settings.Default.Properties["Foo"] as string} can result in an exception if the settings name is changed. Also, the casting can fail. I think between duplication of data (user default and app default) and type safety, duplication is the lesser evil here
-
Ohad Schneider over 14 yearsBTW, making sure you never write to the default settings is easy - make all settings there application scoped, rather than user scoped
-
ChrisJJ over 12 yearsI find "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." is not always true. E.g. After .Save then .Reset, .Reload does not load the set saved by .Save - it has no effect.
-
Kyle Delaney almost 7 yearsWhat if you want to reset only one setting and not all of them?
-
Ohad Schneider almost 7 years@KyleDelaney looks like aku's answer above allows that (stackoverflow.com/a/49289/67824).
-
Bob Denny over 6 yearsThis only works for scalars. I have a property that is of type System.Collections.Specialized.StringCollection. Using the above returns the raw XML, requiring you to pick through it and create the collection manually.
-
ChihHao over 5 yearsHi @BobDenny, would you explain how you do it?