Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing settings in Properties.Settings.Default vs the Registry [duplicate]

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}

like image 329
clifford.duke Avatar asked Apr 02 '13 12:04

clifford.duke


People also ask

Where are properties settings default stored?

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.

Where are C# application settings saved?

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.

How can I save application settings in a Windows Forms application?

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.


3 Answers

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.

like image 108
Dan Busha Avatar answered Oct 02 '22 11:10

Dan Busha


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.

like image 45
Captain Kenpachi Avatar answered Oct 02 '22 11:10

Captain Kenpachi


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.

like image 21
mnme Avatar answered Oct 02 '22 12:10

mnme