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?

Shirish11
  • 1,469

6 Answers6

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.

Daniel B
  • 6,204
  • 1
  • 24
  • 30
16

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?

yannis
  • 39,647
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.

Robert Harvey
  • 200,592
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).

Daniel B
  • 6,204
  • 1
  • 24
  • 30
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.

Pieter B
  • 13,310
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.