using Properties.Settings for application settings

我怕爱的太早我们不能终老 提交于 2019-12-08 16:01:10

问题


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.


回答1:


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...




回答2:


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.



来源:https://stackoverflow.com/questions/8614360/using-properties-settings-for-application-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!