Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

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

6 Answers 6

up vote 19 down vote accepted

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.

share|improve this answer
    
I used SimpleIni for parsing INI files a while ago and it was working nicely. –  Jonathan Merlet Apr 16 '12 at 13:16
    
@JonathanMerlet thanks, I've added SimpleIni to the post. –  Daniel B Apr 16 '12 at 13:56

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
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 Apr 13 '12 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 Apr 16 '12 at 7:52
    
Also, registry can have size limit. Files don't. –  linquize Apr 16 '13 at 6:12

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?

share|improve this answer
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. –  Robert Harvey Apr 13 '12 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 Rizos Apr 13 '12 at 15:57
1  
XML can be made nicer by rolling values into attributes. For example, <window width="600" height="350" /> –  Robert Harvey Apr 13 '12 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 Apr 16 '12 at 5:29
    
@DanielB Someone did mention YAML ;) –  Yannis Rizos Apr 16 '12 at 5:34

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.

share|improve this answer
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 Apr 13 '12 at 8:47
1  
@Manjabes: Characteristics which are unlikely to be significant for configuration files. –  Robert Harvey Apr 13 '12 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 Apr 16 '12 at 14:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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