Registry vs. INI file for storing user configurabl

2019-01-13 15:09发布

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?

12条回答
我只想做你的唯一
2楼-- · 2019-01-13 15:11

The existing answers cover a lot of ground but I thought I would mention one other point.

I use the registry to store system-wide settings. That is, when 2 or more programs need the exact same setting. In other words, a setting shared by several programs.

In all other cases I use a local config file that sits either in the same path as the executable or one level down (in a Configuration directory). The reasons are already covered in other answers (portable, can be edited with a text editor etc).

Why put system-wide settings into the registry? Well, I found that if a setting is shared but you use local config files you end up duplicating settings. This may mean you end up needing to change a setting in multiple places.

For example, say Program A and Program B both point to the same database. You can have a "system-wide" registry setting for the connection string. If you want to point to a different database, you can change the connection string in one place, and both programs will now run against the other database.

Note - there is no point in using the registry in this way if two or more programs don't need to use the same values. Such as, Program A and Program B both needing a database connection string that may be the same, but not always. For example, I want Program B to now use a test database but Program A should carry on using a production database.

With the above example, you could have some local configuration override system-wide settings but it may start getting overly complicated for simple tasks.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-13 15:14

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.

查看更多
The star\"
4楼-- · 2019-01-13 15:15

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.

查看更多
SAY GOODBYE
5楼-- · 2019-01-13 15:19

Jeff Atwood has a great article about Windows' registry and why is better to use .INI files instead.

My life would be a heck of a lot easier if per-application settings were stored in a place I could easily see them, manipulate them, and back them up. Like, say... in INI files.

  • The registry is a single point of failure. That's why every single registry editing tip you'll ever find starts with a big fat screaming disclaimer about how you can break your computer with regedit.
  • The registry is opaque and binary. As much as I dislike the angle bracket tax, at least XML config files are reasonably human-readable, and they allow as many comments as you see fit.
  • The registry has to be in sync with the filesystem. Delete an application without "uninstalling" it and you're left with stale registry cruft. Or if an app has a poorly written uninstaller. The filesystem is no longer the statement of record-- it has to be kept in sync with the registry somehow. It's a total violation of the DRY principle.
  • The registry is monolithic. Let's say you wanted to move an application to a different path on your machine, or even to a different machine altogether. Good luck extracting the relevant settings for that one particular application from the giant registry tarball. A given application typically has dozens of settings strewn all over the registry.
查看更多
祖国的老花朵
6楼-- · 2019-01-13 15:20

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#.

查看更多
甜甜的少女心
7楼-- · 2019-01-13 15:27

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.

查看更多
登录 后发表回答