Registry vs. INI file for storing user configurable application settings [closed]
Asked Answered
H

13

55

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?

Heptode answered 9/8, 2008 at 4:13 Comment(0)
C
45

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.

Claman answered 9/8, 2008 at 4:51 Comment(4)
JSON is another widely used format for simple data files nowadays.Caeoma
missing points: re-install of fresh windows dont kill ini files. A centralized database needs to be mutexed, and we are not talking Oracle quality with multi level rollbackable transactions here. Cleaner/antivirus tools love to mess with people's registry, not so much scattered ini files. the registry is just a filesystem in a filesystem, this is an antipattern en.wikipedia.org/wiki/Inner-platform_effect...Neritic
INI files are a better system. The application upon a successful boot can simply save a backup of the INI to protect against corruption.Glyceric
Another point on 'portable' is that application settings can be kept in a .ini file in the application directory itself, so if (possibly multiple versions/copies of) the application run from a flash drive or network share, settings stored in the .ini files stay with the application, and therefore possibly with the user's removable storage, rather than with the computer. You can also keep multiple versions of the .ini files and revert to one if they get corrupted.Idette
B
28

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.
Bolten answered 11/10, 2008 at 1:11 Comment(1)
I totally agree. The registry just reeks of garbage and rinky-dink apps that you can never completely delete.Borosilicate
J
5

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, they can get the INI file to be encrypted pretty easily. With the registry it will probably be more problematic.

Jacobian answered 24/8, 2008 at 14:41 Comment(0)
A
4

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!

Aegrotat answered 9/8, 2008 at 4:54 Comment(0)
G
4

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.

Gigi answered 9/8, 2008 at 13:3 Comment(0)
N
4

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.

Ng answered 11/10, 2008 at 2:14 Comment(2)
This makes really sense for portable applications, whereas application installed into the %PROGRAMFILES% folder should better use a subfolder in %APPDATA% that is mostly named after (company and) application.Fanny
That is lost after a OS reload.Ng
S
3

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

Sideband answered 9/8, 2008 at 13:13 Comment(1)
There's a whole question on this (for c#).Refute
E
2

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.

Erlindaerline answered 24/8, 2008 at 9:1 Comment(0)
T
2

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.

Tiber answered 21/1, 2016 at 10:25 Comment(2)
This is what I wanted to say. If I want to add an extension to chrome or firefox as part of my installation, the way to do that is to add registry entries that chrome/firefox expects to find. This also applies to registering with some app as a plugin/helper etc. Because the other app may not be running at the time any kind of IPC wont work. Also if you search for the app, you don't know if it is the version that is being used or a backup. Registry is the way to go here.Tufa
There is "Program Data" folder for "system-wide" shared config files.Harrietharriett
C
1

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.

Cluj answered 11/10, 2008 at 1:40 Comment(1)
you're expected to put your ini in %appdata%.Neritic
P
0

There is one drawback to ini or config files and that is locating them if the user has the option to select where the program is installed.

Publicist answered 27/1, 2011 at 2:52 Comment(4)
Put it in the same directory as the exe. Pull the exe directory from argV[0].Ng
You can't rely on argv[0]. It can be absolute, relative, or completely empty or completely wrong (for example, if a process is launched via exec*). Use GetModuleFileName with a NULL first param instead followed by PathCanonicalize to remove extraneous path elements.Kerakerala
...and then its also not perfect because it can be a symlink. you need to finish that using GetFinalPathNameByHandle.Neritic
Just put 'em in `%APPDATA%\YOURAPP`Miramontes
R
0

Other disadvantage of using the registry is that it is a pain if you are working in a mixed environment of 32 and 64 bit applications, as the system call for accessing the registry will randomly(*) add \Wow6432Node\ to your registry path making you crazy while debugging.

(*of course not randomly, but very easy to get lost)

Raffish answered 26/6, 2019 at 12:39 Comment(0)
A
0

Advantages:

  • Replacement for a large number of configuration files.
  • Common administrative functions at a central point.
  • Almost any data can be saved by applications / drivers.
  • In contrast to configuration files, code sequences can even be saved.
  • Access faster than files because the database is indexed.
  • Access can be logged using the RegMon utility

Disadvantages:

  • Difficult to work with in the absence of graphical configuration programs.
  • Direct changes using the registry editor can create inconsistent states produce.
  • Incomplete uninstallers leave “reminiscences” in the registry Cause problems, e.g. with a new installation.
  • Installed applications are difficult to export to other PCs.
  • Chronically poorly documented.
  • Proprietary structure, therefore not suitable for standard DB access (e.g. SQL)
  • Computer-specific, therefore not portable to other computers.
  • Insufficient protection of the registry: depends on the configuration.
Arnone answered 3/6, 2021 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.