2

Where is the best place to store application preferences?

In particular, I'd like to save preferences for a media player such as volume levels and the like. Two candidates spring to mind... file and registry. Which would be more appropriate?

As a follow up to this, I'm also wondering if there are any APIs that aid in creating application specific settings.

Unless someone advises me that this is wrong, I'd like to save stuff either in HKCU... or HKLM/Software/MyCompanyName/MyAppName/Key for the registry, or in %APPDATA\MyCompanyName\MyAppName\someTypeOfSettingsFile.

As these seem to be commonly used for such settings, I'd assume that .Net makes it easy to store settings in these locations. Is there a simple high level API that can .Net offer me to read and write settings to these common locations?

spender
  • 117,338
  • 33
  • 229
  • 351

2 Answers2

5

Open settings.settings in your Visual Studio solution; add an application setting (i.e. change scope from 'user' to 'application').

The IDE creates a backing class that you can use in code.

The registry should be avoided.

The location of the app.exe.config and user.config files will differ based on how the application is installed. For a Windows Forms-based application copied onto the local computer, app.exe.config will reside in the same directory as the base directory of the application's main executable file, and user.config will reside in the location specified by the Application.LocalUserAppDataPath property. For an application installed by means of ClickOnce, both of these files will reside in the ClickOnce Data Directory underneath %InstallRoot%\Documents and Settings\username\Local Settings.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • thanks for taking the time to reply. So this saves in the application .config file right? And this would live in the users programs/program files folder? And this is reliable? I'm confused because I can't understand how this squares with the idea that users might not have write access to that particular location. What don't I understand? – spender Dec 23 '10 at 00:42
  • 3
    This file can he hosted in the application data folder for the user, where every user has write access. You don't have to assume anything about the users' configuration, the .net framework takes care of it for you. – Blindy Dec 23 '10 at 00:45
  • Great. Good to go then. Thanks. – spender Dec 23 '10 at 00:47
1

See Application Settings for Windows Forms. Even though it is about Windows Forms, this can be used for other similar applications.

Don't use the Registry without very good reason.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Why is using the registry bad form? – spender Dec 23 '10 at 00:43
  • 2
    Because who wants to deal with all the "Registry" crap just for a single application? Why have to go into a program (registry viewer) just to see application settings? – John Saunders Dec 23 '10 at 00:48
  • Agree with John S for single application. But on the other hand, one possibly good reason for using the registry, in scenarios where the settings are shared between apps or between users, is that each registry key can have its own ACL, so a fine-grained security model is possible, whereas a config file is all or nothing. – Chris Dickson Dec 23 '10 at 15:26
  • @Chris: those are fairly rare situations. For "Application Preferences", which is what the question was about, a settings file is the way to go, and the registry is not. – John Saunders Dec 23 '10 at 15:45
  • Yes, you are quite right - in commenting I didn't stay within the scope of the question. I accept that my comment is only relevant to a more general consideration of config file v registry: config files are often (too often IMO) used for things which directly control application logic rather than just user preferences. I'd accept this is much more relevant to server applications than client side applications like a media player. – Chris Dickson Dec 23 '10 at 16:07