Which method is considered to be best practice for setting storage for applications: XML or Registry? What are the benefits/downsides of using either of these?
4 Answers
The registry is Microsoft's solution to storing application information relative to a user. The advantages of this include:
- Being able to hold application settings for each user of the computer.
- Having an API already available for using this feature.
However the disadvantages include:
- Platform dependent.
- Limited to the amount of information for a single block of data.
- Loss of portability (you lose settings when you install on another machine)
I personally vouche for using XMLs if not merely for the fact that it allows you to copy the entire application to another computer and have it work fine with the same settings. Plus, if you write your application flexible enough, you can write entire projects onto XML files, something I wouldn't dare attempt with a registry.
It's convenient initially to use the registry, but it's far better in the long run to use XML files in my opinion.
For information that may be needed by other programs, such as the location and current version of an executable, store that in the registry.
For information that is only needed by your application, store that in a file in the user's AppData directory. Use XML or whatever you find suitable, depending on the structure of the data.
That allows the registry to do its job - function as a central registry of applications - without cluttering it up.
It'll also make your testing life a little easier, since you can wipe the AppData file and reset the app back to defaults.
A major downside of the registry in Windows Vista and forward is that you can only store stuff in HKEY_CURRENT_USER unless you're running elevated.
That means that if your non-elevated program stores stuff in HKCU, and the user installs a new version of the program, the installer likely runs elevated, which may mean running under a different user account.
That means that you can't rely on being able to read program information out of the registry from the installer, because the installer is running under a different user, who has different stuff in their registry. :(
On Windows, I feel program settings belong in the registry. While xml-files may be easier to copy by hand, backup and restore procedures routinely include the registry, and they also support multiuser setups, where program install directories may not be preserved (nor would I want that, because paths may not be the same, and installers usually also take care of associating files and creating shortcuts. So on Windows, I mostly prefer installers over copying directories. A function (from the gui) to export and import settings may really help migration though.
Project settings are perhaps better stored in the document or project directory. That allows for version control and sharing across users.
-
1The whole U3 application (flash drive installation) thing relies on not having things in the registry, so for portable apps even on Windows the registry might not be the best choice.– DaveEMar 10, 2011 at 21:58
.ini
file. XML is a heavy, flexible format, powerful enough for tasks like storing a whole database for import/export. But it's overkill for simple key-value pairs and other simple relations. Especially when you'd need to add an XML parser for the config files alone.