Reputation: 10619
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:
CellBackgroundColor
in Properties.Settings
.CellBackgroundColor
to Color.White
using the IDE.CellBackgroundColor
to Color.Black
in my program.Properties.Settings.Default.Save()
.Restore Default Colors
button.Now, Properties.Settings.Default.CellBackgroundColor
returns Color.Black
. How do I go back to Color.White
?
Upvotes: 30
Views: 47096
Reputation: 27214
I found that calling ApplicationSettingsBase.Reset
would have the effect of resetting the settings to their default values, but also saving them at the same time.
The behaviour I wanted was to reset them to default values but not to save them (so that if the user did not like the defaults, until they were saved they could revert them back).
I wrote an extension method that was suitable for my purposes:
using System;
using System.Configuration;
namespace YourApplication.Extensions
{
public static class ExtensionsApplicationSettingsBase
{
public static void LoadDefaults(this ApplicationSettingsBase that)
{
foreach (SettingsProperty settingsProperty in that.Properties)
{
that[settingsProperty.Name] =
Convert.ChangeType(settingsProperty.DefaultValue,
settingsProperty.PropertyType);
}
}
}
}
Upvotes: 1
Reputation: 2373
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");
Upvotes: 5
Reputation: 133
Properties.Settings.Default.Reset()
will reset all settings to their original value.
Upvotes: 3
Reputation: 38106
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()
Upvotes: 14
Reputation: 10291
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.
Upvotes: 2
Reputation: 123966
@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"
Upvotes: 42
Reputation: 16758
How do I go back to Color.White?
Two ways you can do:
Upvotes: 1