What is the difference between storing user settings for an application in
Properties.Settings.Default
versus storing it in the registry say
HKEY_CURRENT_USER\Software\{Application Name}
What is the difference between storing user settings for an application in
Properties.Settings.Default
versus storing it in the registry say
HKEY_CURRENT_USER\Software\{Application Name}
Properties.Settings.Default
A common technique for storing application state between executions is to write it to disk. There are a wide variety of approaches for doing this and Microsoft jumped into the game by introducing the classes in System.Configuration namespace to help users manage saving application state (settings).
Properties.Settings.Default
is a static instance of a class that derives from ApplicationSettingBase which manages the reading and writing of settings to disk. Properties tagged with the [UserScopedSetting]
attribute are saved to an XML file in C:\Users\user\AppData\Local\ComapnyName\Hashed_AppName\version which can be read and written to by the user. Properies tagged with the [ApplicationScopedSetting]
attribute are saved to app.config file and can only be read.
A basic Settings file looks something like this:
class FormSettings : ApplicationSettingsBase
{
public WindowSettings() {}
public WindowSettings(string settingsKey) : base(settingsKey) {}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("MyDefaultName")]
public String Name
{
get { return (string)(this["Name"]); }
set { this["Name"] = value; }
}
}
You can set values in Properties.Settings.Default
either in the UI, under Project Properties->Settings or programattically via Properties.Settings.Default
.
Registry
The Registry is a hierarchical database that stores configuration settings and options and stores these them as key value pairs. See Wikipedia for more information.
The registry can access be accessed through the static Microsoft.Win32.Registry class which will allow you to read and write values. For example:
public class RegistryExample
{
public static void Main()
{
const string rootUser = "HKEY_CURRENT_USER";
const string subkey = "RegistryExample";
const string keyName = String.Format("{0}\\{1}, rootUser, subkey);
Registry.SetValue(keyName, "MyRegistryValue", 1234);
}
}
See the MSDN documentation page for examples. As others have mentioned there are pros and cons to using the registry, but I think it's worth stating again that the registry is a "secure" location and your users will need permission to read a write from it where as a settings file does not require those permissions.
The difference is that the registry is, well, the registry. Whereas Properties.Settings.Default saves to a config file in your AppData directory.
I personally don't like working with the Registry at all. It's just a phobia I have left over from the Windows 98 era. It's not a nice experience working with the registry anyway. The key names are ugly and there's lots of potential for causing chaos.
From codehill.com:
Before the .NET Framework application settings were saved in INI files and the Windows Registry. But the .NET Framework introduced a much easier way using an XML file. The file has the name of the Assembly and a .exe.config extension, and is placed inside the application’s folder. This way is cleaner because when an application is uninstalled or deleted the end user does not have to worry about left over registry keys or INI files in the Windows directory.
Saving settings like Properties.Settings.Default is the one and only properly way to do it.
The registry is a no-go. You're not sure whether the user which uses your application, has sufficient rights to write to the registry.