Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Properties.Settings for application settings

I use the built-in settings provided by Visual Studio to store simple application settings. Until now, I've accessed this in my application by using the convention:

Properties.Settings.Default.MySetting

And then call methods like Save by using:

Properties.Settings.Default.Save()

However, someone recently told me that it is more correct to access the properties by creating a member variable like this:

private Properties.Settings settings = new Properties.Settings()

And then using the member settings to access properties and methods like:

settings.MySetting
settings.Save()

I vaguely recall that they justified this by describing differences in the way the settings are stored in the user's area.

Can anyone confirm or give further details on the differences? Many thanks.

like image 952
millie Avatar asked Dec 23 '11 09:12

millie


2 Answers

Settings.Default is initialized as follows:

private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

So it's almost the same as manually creating an instance of Settings, except that the one provided by Settings.Default is a synchronized instance. I can't see any good reason to create an instance of Settings manually...

like image 50
Thomas Levesque Avatar answered Sep 19 '22 04:09

Thomas Levesque


This wasted a lot of my time.

[MyAppNameSpace].Properties.Settings.Default.Save();

Not sure when you can drop the namespace as above but in wpf in the app.xaml.cs code I needed to specify the namespace to get it to compile.

like image 22
Dirk Bester Avatar answered Sep 18 '22 04:09

Dirk Bester