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}
There is a folder called "Properties" under your project root folder, and there are *. settings file under that folder. That's where it gets stored.
Settings. settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there.
A simple way is to use a configuration data object, save it as an XML file with the name of the application in the local Folder and on startup read it back.
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}, userRoot, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With