A registry or INI file for storing user-configurable application settings

I am a new Windows programmer, and I am not sure where I should store user customizable application settings. I understand the need to provide users with convenient tools for changing application settings, for example Edit | Settings form or similar. But where should I store the values ​​after the user clicks the "Apply" button on this form?

What are the pros and cons of saving settings in the Windows registry and saving them in a local INI file or configuration file or similar?

+46
windows settings configuration-files registry ini
Aug 09 '08 at 4:13
source share
14 answers

Pluses of the configuration file:

  • Easy to do. No need to know any Windows API calls. You just need to know the file I / O interface of your programming language.
  • Portable. If you transfer the application to another OS, you do not need to change your settings format.
  • User editable. The user can edit the configuration file outside the executable program.

Registry advantages:

  • Secure A user cannot accidentally delete a configuration file or corrupt data if he does not know about regedit. And then the user just asks for trouble.
  • I am not an expert on Windows programming, but I am sure that using the registry makes it easier to work with other Windows subjects (user settings, network administration, such as group policy or something else).

If you just need an easy way to store configuration information, I would recommend a configuration file using INI or XML as the format. I suggest using the registry only if there is something specific from which you want to refuse to use the registry.

+39
Aug 09 '08 at 4:51
source share

Jeff Atwood has an excellent article on the Windows registry and why it is better to use .INI files.

My life would be much simpler if the application settings were saved in a place where I could easily see them, manipulate them and create backups. For example, say ... in .ini files.

  • The registry is the only point of failure . Therefore, every registry editing tip you will ever find begins with a big thick screaming reservation about how you can break your computer with regedit.
  • The registry is opaque and binary . As far as I don't like the tax with angle brackets, at least the XML configuration files are human-readable enough and they allow as many comments as they see fit.
  • The registry must be synchronized with the file system . Uninstall the application without β€œdeleting” it, and you will be left with an outdated registry. Or, if the application has a poorly written uninstaller. The file system is no longer an expression about a record - it needs to be somehow synchronized with the registry. This is a complete violation of the DRY principle.
  • The registry is monolithic . Let's say you wanted to transfer the application to a different path to your computer or even to another machine. Good luck extracting the appropriate settings for one specific application from the giant tarball registry. This application usually has dozens of settings scattered throughout the registry.
+24
Oct 11 '08 at 1:11
source share

According to the documentation for GetPrivateProfileString , you should use the registry to store initialization information.

However, if you still want to use .ini files and use the standard profile APIs ( GetPrivateProfileString , WritePrivateProfileString , etc.) to access them, they provide built-in ways to automatically provide "virtual .ini files" supported by the registry . Win-win!

+4
Aug 09 '08 at 4:54
source share

There is a similar question that covers some of the pros and cons.

I would advise against using the registry if this application does not need it. In my opinion, Microsoft is trying to refuse to use the registry due to the flexibility of the configuration files. In addition, I would not recommend using .ini files, but instead used some of the built-in functions for .Net to save the user / application settings.

+4
Aug 09 '08 at 13:03
source share

Using an ini file in the same directory as the application allows you to back up the application. Therefore, after rebooting the OS, you simply restore the application directory, and you have the configuration the way you want.

+4
Oct 11 '08 at 2:14
source share

There is another advantage of using an INI file over the registry, which I did not mention: If the user uses some kind of encryption based on volumes and files, they can easily encrypt the INI file. With the registry, this is likely to be more problematic.

+3
Aug 24 '08 at 14:41
source share

I agree with Daniel. If this is a great application, I think I'm doing something in the registry. If this is a small application, and you want its aspects to be configured by the user without creating a configuration form, go to the quick INI file.

I usually understand this way (if the format in the .ini file has option = value, 1 per line, comments start with C #):

 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 overwritten. config[kvPair[0].Trim()] = kvPair[1].Trim(); } } 

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

+2
Aug 9 '08 at 13:13
source share

As Daniel noted, storing configuration data in the registry gives you the ability to use admin templates. That is, you can define an administrator template, use it in Group Policy and administer the configuration of your application throughout the network. Depending on the nature of the application, this can be a great boon.

+2
Aug 24 '08 at 9:01
source share

The existing answers cover a lot of reasons, but I thought I mentioned one more thing.

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

In all other cases, I use a local configuration file, which is either on the same path as the executable, or one level down (in the configuration directory). The reasons have already been discussed in other answers (portable, you can edit using a text editor, etc.).

Why install system settings in the registry? Well, I found that if the parameter is shared, but you use local configuration files, you duplicate the settings. This may mean that you need to change the setting in several places.

For example, let's say program A and program B point to the same database. You may have a "system" registry setting for the connection string. If you want to specify a different database, you can change the connection string in one place, and both programs will run in another database.

Note. It makes no sense to use the registry in this way if two or more programs do not need to use the same values. For example, program A and program B, which require a database connection string, may be the same, but not always. For example, I want program B to use the test database now, but program A should continue to work with the production database.

In the example above, you may have a local setting overriding system-wide settings, but it may become too complex for simple tasks.

+2
Jan 21 '16 at 10:25
source share

The registry is optimized for quick access and easy updates, and this is the only way to perform certain Windows-related actions, such as binding to an extension. And you can ignore the argument of deleting one directory to delete your program - Windows Vista will not allow you to change the files in the Program Files folder, so your configuration will have to go to a different folder anyway.

There is a general guide for programming Windows - do what Microsoft expects of you, and your life will be much easier.

However, I see the appeal of the INI file, and I would not blame anyone for that.

+1
Oct 11 '08 at 1:40
source share

There is one drawback of ini or config files and their search if the user has the opportunity to choose where the program is installed.

0
Jan 27 '11 at 2:52
source share

There is no single right choice. In the Windows world, both have their purpose. When you hear a dogma (strong unfounded opinion and religious use of terms such as antipattern), ignore it unless the person using them has understood the problem you are trying to solve. (There is no scheme for getting from A to B; you can walk, ride a bicycle, taxi, train, plane, boat, etc. When you know what you need - say - cross the ocean, and then listen specific tips for continental travel.)

If you write system software (Windows services, device drivers, COM components ... that is, software that is "close to the machine"), then the registry is probably what you need. Configuration files go well with the user interface - especially where the user may want or need to control the look.

There are far more negative opinions about the registry than the people who used it (respectively). Not surprisingly, since most opinions are based on the past of the person offering it, and there are many more UI programmers.

My advice: as a beginner, use a simple configuration file; There are many examples, and when you want to hide or protect your settings, examine the registry.

0
Jun 11 '19 at 9:39 on
source share

Another disadvantage of using the registry is that if you are working in a mixed environment with 32- and 64-bit applications, this is \Wow6432Node\ , since the system call to access the registry randomly (*) adds \Wow6432Node\ to your path in the registry, making you crazy while debugging.

(* certainly not by chance, but it is very easy to get lost)

0
Jun 26 '19 at 12:39 on
source share

Is your application the one that is installed with the installer, or is it just "Uninstall and Run"? In the first case, consider the pros and cons set forth here. But for Extract and run, the Registry, in my opinion, is not-go, since people expect that they can simply delete the application folder to get rid of your program.

-2
Aug 09 '08 at 13:16
source share



All Articles