19

I want to save the configuration of my project which includes

  1. Screen size
  2. Screen position
  3. Folder paths
  4. Users settings and so on.

The standard places where you can save these are configuration values are:

  1. Registry
  2. INI files
  3. Personal files (like *.cfg)

How do you choose between these places? Also, are there any pros and cons of using any of them?

4
  • 2
    What tools are you using? Some technologies comes with pretty nice build in configuration management tools.
    – user2567
    Commented Apr 13, 2012 at 7:17
  • @Pierre303 currently using VS 2008 for VC++
    – Shirish11
    Commented Apr 13, 2012 at 7:41
  • Will users need to make changes?
    – JeffO
    Commented Apr 13, 2012 at 15:03
  • 1
    Have you considered YAML, you get the best of both, ini and xml en.wikipedia.org/wiki/YAML
    – JF Dion
    Commented Apr 13, 2012 at 15:43

6 Answers 6

20

Also are there any pros and cons of using any of them?

Registry:

  • + Relatively standard in the Windows environment.
  • + Generally good support from installers, etc.
  • - Platform specific API, if you ever want to port your application.
  • - Not particularly human readable.

INI Files:

  • + Simple format.
  • + Portable.
  • + Human readable.
  • - May be difficult to store more complex information (for example, anything nested more than two levels deep).
  • ? May have to write your own parser (although not difficult) or use an external library like SimpleIni (thanks to Jonathan Merlet for the comment).

XML Files (I'm guessing this is the .cfg option):

  • + A standard format.
  • + Portable.
  • + Supports deeply nested structures.
  • - Not particularly human readable.

Personally, for Windows applications I tend to use C#, and go with a personal file for the user, stored as XML. I do this typically because human readability is not usually a priority in the types of applications I write (and the application should have a configuration editor, in any case), and in the .NET environment, it's very easy to work with XML. I very often end up with a UserConfiguration object which simply gets serialised to and from a configuration file - almost no development involved (parsing, casting stuff around), and you have your configuration ready to use in a strongly typed environment.

2
  • I used SimpleIni for parsing INI files a while ago and it was working nicely. Commented Apr 16, 2012 at 13:16
  • @JonathanMerlet thanks, I've added SimpleIni to the post.
    – Daniel B
    Commented Apr 16, 2012 at 13:56
15

I'd go with INI files, they are the more human friendly option:

[window]
width       = 600
height      = 350
position.x  = 400
position.y  = 200

[paths]
path1       = "/some/random/path/"
path2       = "/some/other/random/path/"

[user]
name        = "Yannis"
preference  = "INI"

XML might be a good option, but it can't beat INIs' simplicity and elegance:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <window>
        <width>600</width>
        <height>350</height>
        <position>
            <x>400</x>
            <y>200</y>
        </position>
    </window>
    <paths>
        <path1>/some/random/path/</path1>
        <path2>/some/other/random/path/</path1>
    </paths>
    <user>
        <name>Robert</name>
        <preference>XML</preference>
    </user>
</configuration>

INIs are also very well understood and platform independent. There aren't probably as many tools available for them as for XML files, because, well, who needs a specialized tool to read and edit an INI file?

8
  • 2
    Funny, but I find the XML nicer looking and easier to read. For those that don't like the verbosity, JSON is a suitable alternative. Commented Apr 13, 2012 at 15:38
  • 3
    @RobertHarvey JSON I like (and would use if I needed deeper nesting), but XML is really not my cup of tea...
    – yannis
    Commented Apr 13, 2012 at 15:57
  • 3
    XML can be made nicer by rolling values into attributes. For example, <window width="600" height="350" /> Commented Apr 13, 2012 at 16:01
  • I'm actually surprised nobody's mentioned YAML yet. I feel it's a touch more human friendly than JSON, but quite similar in capability and syntax. The yaml.org website probably scares away anyone not already familiar with it though.
    – Daniel B
    Commented Apr 16, 2012 at 5:29
  • @DanielB Someone did mention YAML ;)
    – yannis
    Commented Apr 16, 2012 at 5:34
6

My preference is XML files. They are hierarchical, you can bend them to your will in almost any way imaginable, they are well understood, platform independent, and there is a wide array of software available to read and write them.

3
  • 6
    But horribly verbose and tend to be not human-readable afterall (think not indented or bloated to the max with namespace declarations and other bs).
    – Manjabes
    Commented Apr 13, 2012 at 8:47
  • 1
    @Manjabes: Characteristics which are unlikely to be significant for configuration files. Commented Apr 13, 2012 at 15:22
  • @robert harvey : very significant where I live. For the basic meddling with settings you use the gui, for the advanced settings you go edit the ini.
    – Pieter B
    Commented Apr 16, 2012 at 14:07
6

To expand Jeff D's suggestion of YAML, here's a brief intro.

YAML is similar to JSON (in fact, JSON is a subset of YAML since version 1.2 of the YAML standard, and can thus valid JSON can be parsed by YAML parsers). At first glance, the main difference being that YAML (by default) uses indentation rather than bracketing to show hierarchy. There is also a noticable lack of quotes for strings. A quick example from above:

configuration:
  window: 
    width:       600
    height:      350
    position:
      x:         400
      y:         200
  paths:
    path1:       some/random/path
    path2:       some/other/random/path
  user:
    name:        Joe Soap
    preference:  YAML

See Wikipedia's YAML page for better examples. Support for YAML exists for most major languages (see yaml.org for details).

3

You forgot an option: the Database. This is mostly used in scenarios where you have users logging into your application when your application can't rely on the logged in windows user. This is for instance when your app is running in a kiosk mode windows.

2
  • You may need an INI file or registry key to hold the connection details/string to the database Commented Jan 21, 2016 at 10:00
  • @CamelCase Inifile or registry key was already mentioned in the Question, but not Database. I didn't mean to say that you can't use multiple methods at the same time.
    – Pieter B
    Commented Jan 21, 2016 at 10:34
1

My personal preference is XML files:

In most cases I don't expect the user to have to edit their configuration settings so the human readability issue is not an argument in this case.

If they do need to edit them you can provide a editing tool - this prevents the user doing something daft with the data. If they want to restore the default settings you can just tell them to delete file x which most users will be comfortable doing.

Note that you still need to be careful that you have permission to store your file as some locations do not have write access by default in Windows 7 etc.


INI Files are a good standard way of storing configuration and are tried and tested but they just feel a bit 'Windows 3.1' to me!

Probably the best option if you want the user to be able to tinker with their data


I would personally steer away from the registry. For one thing you cannot guarantee that the user has the necessary permissions to read/write to wherever you want to store your data.

In more recent OSs where registry virtualisation comes into play this can cause major confusion because you can't 'see' the virtualised settings - this has bitten us more than once where have spent hours trying to figure out why something was not working.

3
  • 3
    If you're worried about Permissions for registry locations, then you're probably trying to store them in the wrong place. The user should have permissions to the HKey_Current_User hive, and that's where per-user settings should be!
    – Neil White
    Commented Apr 13, 2012 at 16:51
  • @NeilWhite - agreed they should have permissions but an over zealous IT administrator can change this (or give read/write but not create permissions). Also the OP did not mention that these settings were necessarily per user, they may be per machine.
    – Matt Wilko
    Commented Apr 16, 2012 at 7:52
  • Also, registry can have size limit. Files don't.
    – linquize
    Commented Apr 16, 2013 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.