views:

2124

answers:

10

I'm a new Windows programmer and I'm not sure where I should store user configurable application settings. I understand the need to provide a user friendly means for the user to change application settings, like an Edit | Settings form or similar. But where should I store the values after the user hits the Apply button on that form?

What are the pros and cons of storing settings in the Windows registry vs. storing them in a local INI file or config file or similar?

+10  A: 

Pros of config file:
1. Easy to do. Don't need to know any Windows API calls. You just need to know the file I/O interface of your programming language.
2. Portable. If you port your application to another OS, you don't need to change your settings format.
3. User-editable. The user can edit the config file outside of the program executing.

Pros of registry:
1. Secure. The user can't accidentally delete the config file or corrupt the data unless he/she knows about regedit. And then the user is just asking for trouble.
2. I'm no expert Windows programmer, but I'm sure that using the registry makes it easier to do other Windows-specific things (user-specific settings, network administration stuff like group policy, or whatever else).

If you just need a simple way to store config information, I would recommend a config file, using INI or XML as the format. I suggest using the registry only if there is something specific you want to get out of using the registry.

Daniel F. Hanson
+2  A: 

According to the documentation for GetPrivateProfileString, you should use the registry for storing initialisation information.

However, in so saying, if you still want to use .ini files, and use the standard profile APIs (GetPrivateProfileString, WritePrivateProfileString, and the like) for accessing them, they provide built-in ways to automatically provide "virtual .ini files" backed by the registry. Win-win!

Chris Jester-Young
+3  A: 

There's a similar question here that covers some of the pros and cons.

I would suggest not using the registry unless your application absolutely needs it. From my understanding, Microsoft is trying to discourage the use of the registry due to the flexibility of settings files. Also, I wouldn't recommend using .ini files, but instead using some of the built-in functionality to .Net for saving user/app settings.

Greg
+1  A: 

I agree with Daniel. If it's a large application I think I'd do things in the registry. If it's a small application and you want to have aspects of it user-configurable without making a configuration form, go for a quick INI file.

I usually do the parsing like this (if the format in the .ini file is option = value, 1 per line, comments starting with #):

    static void Parse()
{
StreamReader tr = new StreamReader("config.ini");
string line;
Dictionary<string, string> config = new Dictionary<string, string>();

while ((line = tr.ReadLine()) != null)
{
// Allow for comments and empty lines.
if (line == "" || line.StartsWith("#"))
continue;

string[] kvPair = line.Split('=');

// Format must be option = value.
if (kvPair.Length != 2)
continue;

// If the option already exists, it's overwritten.
config[kvPair[0].Trim()] = kvPair[1].Trim();
}
}

Edit: Sorry, I thought you had specified the language. The implementation above is in C#.

deadtime
A: 

Is your Application one that is installed with a Setup Program, or is it just "Extract and Run"? In the first case, look at the pros and cons outlined here. But for Extract and run, the Registry is in my opinion a "no-go", as people expect to be able to simply delete the application folder to get rid of your program.

Michael Stum
+1  A: 

As Daniel indicated, storing configuration data in the registry gives you the option to use Admin Templates. That is, you can define an Admin Template, use it in a Group Policy and administer the configuration of your application network-wide. Depending on the nature of the application, this can be a big boon.

+1  A: 

There's one more advantage to using an INI file over the registry which I haven't seen mentioned: If the user is using some sort of volume/file based encryption, he can get the INI file to be encrypted pretty easily. With the registry it will probably be more problematic.

On Freund
+3  A: 

Jeff Atwood has a great article about Windows' registry and why is better to use .INI files instead. So I think this article will help you a lot to take a decision.

Lipis
I totally agree. The registry just reeks of garbage and rinky-dink apps that you can never completely delete.
Andy White
A: 

The registry is optimized for quick access and easy update, and it's the only way to do certain Windows-specific things like associating with an extension. And you can ignore the argument about deleting a single directory to uninstall your program - Windows Vista won't let you modify files in the Program Files directory, so your config will need to go in a different folder anyway.

There's a general guideline for Windows programming - do things the way Microsoft expects you to, and your life will be a lot easier.

That said, I can see the appeal of the INI file, and I wouldn't blame anyone for considering it.

Mark Ransom
A: 

Use of an ini file, in the same directory as the application, makes it possible to back it up with the application. So after you reload your OS, you simply restore the application directory, and you have your configuration the way you want it.

EvilTeach