Storing and reading program settings

This is a discussion on Storing and reading program settings within the Windows Programming forums, part of the Platform Specific Boards category; I've got a program which is using a MySQL to connect to database. But in many cases, the MySQL host ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115

    Storing and reading program settings

    I've got a program which is using a MySQL to connect to database. But in many cases, the MySQL host address isn't constant, so I created a dialog where I can change the settings, but here we get to the question:

    What is better type of storing - register of *.ini file (what's more reliable, faster and easier to write - there aren't many settings)?
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well the win32 way is to use the Registry.

    Depending on how widely used your program is, you need to think about how many different copies of your program could be run (on the same machine).
    For example, does it make sense to have settings on a per-user basis, or will all users of the same machine use the same settings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    all users will use the same settings...
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Storing program settings under Windows: A brief overview

    Program settings can either be stored in the registry or a settings file. For settings stored in the registry the following locations should be used.

    Settings for all users should go under:
    Code:
    HKEY_LOCAL_MACHINE\SOFTWARE\YourCompany\YourProgram
    Similarly per-user settings should go under:
    Code:
    HKEY_CURRENT_USER\SOFTWARE\YourCompany\YourProgram
    In nearly all company and many home installations a user will not be able to write to keys under HKEY_LOCAL_MACHINE. Therefore, you should only expect to write there at installation or when an administrator is logged in. If the user has to wait till Wednesday when the part-time sys-admin comes in to change the font size in your application, it is a sure bet that they won't be using your program for long. It is also appropriate for server style software which expects to be administered only by an administrator. Settings under HKEY_LOCAL_MACHINE are shared by all users, which may run into the dozens.

    You read and write to the registry using the registry functions. Search the board with RegCreateKeyEx for examples.

    Although, the use of .ini files was discouraged in favour of the registry when Windows 95 was released, settings files are making somewhat of a comeback. Some users prefer a simple to delete settings file rather than hard-to-find settings in the registry. You can manipulate .ini files with the GetPrivateProfile*/WritePrivateProfile* family of functions. Another increasingly popular format for settings files is xml. Settings files in a binary format should be avoided.

    For settings that apply to all users the settings file should be stored under:
    Code:
    CSIDL_COMMON_APPDATA\YourCompany\YourProgram
    Per user settings should be stored under:
    Code:
    CSIDL_APPDATA\YourCompany\YourProgram
    A CSIDL can be converted to an actual path using the SHGetSpecialFolderPath function. As with the registry, a normal user will not be able to write to files under CSIDL_COMMON_APPDATA.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. Robust method for storing data outside of a program
    By goatslayer in forum C++ Programming
    Replies: 17
    Last Post: 09-19-2007, 03:08 PM
  3. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  4. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  5. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM

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