Best place to store a configuration file

This is a discussion on Best place to store a configuration file within the Windows Programming forums, part of the Platform Specific Boards category; I've got a project that can make use of a user-specified configuration file to modify aspects of its behavior. I'm ...

  1. #1
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978

    Best place to store a configuration file

    I've got a project that can make use of a user-specified configuration file to modify aspects of its behavior. I'm developing on a POSIX platform, so it's just a simple text file that is stashed in the user's home directory (i.e. ~/.configfile, standard Unix behavior).

    This program is written in almost completely portable C, so it builds fine on Windows, at least with MinGW. However, I'm unfamiliar with how Windows handles user config files, so for the time being the program simply looks in the current directory for a file called progname.ini. I'm sure this is not ideal, so I'm looking for the best place, on Windows, to store a configuration file.

    I realize Windows has a registry, but I'm only interested in using a flat file, mainly because this program is firmly rooted in POSIX and it would require a lot more non-portable code to change this behavior, something I'm not interested in. So, given this (presumably less-than-ideal) constraint, how should I go about figuring out what directory this configuration file should go in?

    For reference, the program has a small OS-dependent collection of functions, and this particular function is required to simply return a string containing the name of the preferred configuration file; so right now it's just return "progname.ini";

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cas View Post
    This program is written in almost completely portable C, so it builds fine on Windows, at least with MinGW. However, I'm unfamiliar with how Windows handles user config files, so for the time being the program simply looks in the current directory for a file called progname.ini. I'm sure this is not ideal, so I'm looking for the best place, on Windows, to store a configuration file.
    Windows .INI files are, as a tradition, placed in the same folder as the executable and use progname.ini as the filename.

    The easiest way to do this is...
    Code:
    #inlcude <windows.h>
    
    TCHAR  SetFile[MAX_PATH + sizeof(TCHAR) ];  // max path length + room for a null.
    PTCHAR Ext; 
    FILE *sf;
    
    // get program's name and path
    // note: in console mode you can substitute argv[0]
    GetModuleFilename(NULL,SetFile,MAX_PATH);
    
    // locate file extension
    Ext = strstr(SetFile,".exe"); 
    
    // change to INI
    strcpy(Ext,".ini");
    
    // load settings file
    sf = fopen(SetFile,"r");
        // process settings in here.
    
    // save settings
    sf = fopen(SetFile,"w");
       // process settings out here.
    The nice part about doing it this way is that it allows the user to have multiple copies of the program (by renaming) with different settings...

    For example: if you have MyProgram.exe it produces MyProgram.ini but if you copy and rename the program to MyFancy.exe it will produce MyFancy.ini automatically.
    Last edited by CommonTater; 03-20-2011 at 05:18 PM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Storing a config file in the same folder as the executable is nice because it is very easy. If however you wish to deploy your application to others with say an installer and have it install in some place such as C:\Program Files\ this is not a very good approach. This because how newer windows versions have locked these folders by default so you would have to run your program in an elevated state in order to change the config file.

    For reference how to get environment variables in windows: GetEnvironmentVariable Function (Windows)
    You can use getenv aswell: getenv, _wgetenv (CRT)

    You might be especially interested in the LOCALAPPDATA variable.

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Windows .INI files are, as a tradition, placed in the same folder as the executable and use progname.ini as the filename.
    While that may be, storing it in a global directory under a single filename isn't exactly conducive to multiple user configuration

    Quote Originally Posted by Shakti View Post
    You might be especially interested in the LOCALAPPDATA variable.
    If XP is on your radar and/or it makes sense for your users to carry the config around to any computer they use in a domain, you'll need to use the APPDATA env variable instead.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    Fantastic, thank you all. This was extremely helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21