Click here to Skip to main content
15,885,869 members
Articles / Desktop Programming / MFC
Article

Easy storing application properties in an INI file

Rate me:
Please Sign up or sign in to vote.
3.91/5 (7 votes)
27 Nov 20062 min read 40.6K   720   20   10
An article about CProperties, a helper class to easily store application properties in an INI file.

Screenshot of notepad with some ini properties

Introduction

I mostly use INI files to store program settings. For this, I use a properties class very often. Lately, I optimized it a little bit and I thought that maybe somebody else could also benefit from this class.

The properties class reads the INI file when its object is created. When no INI file exists, it creates it in the same location as where the main application exe is located. The INI file is named the same as the main exe but with the .ini extension. When the object is destroyed, the destructor makes sure that the data is stored in the INI file.

The reason I use INI files more than the registry is because when I have to give support, it is easier for the end user to mail me the INI file with all the settings than have him look into the registry. Sometimes end users are able to make changes to the settings in the INI file themselves. Having users editing the registry is more dangerous than editing an INI file in my opinion.

Using the code

To use this class, you have to add these lines to the stdafx.h:

#include "Properties.h"
extern CProperties prop;

Then add this line to the main application CPP file (myapp.cpp, for example) under the line "CMyApp theApp;":

CProperties prop;

Adding data to the INI file is easy. Just add a public member of type CString, int, or double to the header of CProperties (properties.h) and then add a line like the following in the constructor of CProperties (properties.cpp):

class CProperties  
{
public:
    CString m_serverIp;
     ...    
    
    
CProperties::CProperties()
{
    m_proplist.push_back( 
      t_props("Connection", "ServerIp", "127.0.0.1", &m_serverIp));
                  ^             ^              ^               ^
      key name ___|value name __|default val __| data member __|
    ...

To compile the properties class, add the properties.(cpp/h) and inifile.(cpp/h) files to your project.

To access a property from your source code, you can type something like this.

void MyFunction()
{
    CString server = prop.m_serverIp;       // Get value
    
    prop.m_serverIp = "10.0.0.1";           // Store value
}

Dependency

The actual reading of the ini file is done by the CIniFile class from Shane Hill and can also be found on the Codeproject website. A copy of the source is included in the zip file of this project.

History

  • 1.0 | 27 Nov 2006 | Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalit is still good Pin
Southmountain16-Sep-20 6:20
Southmountain16-Sep-20 6:20 
GeneralForward in using CIniFile class Pin
Raymond_Gimilio30-Apr-08 3:44
Raymond_Gimilio30-Apr-08 3:44 
GeneralA very bad surprise Pin
Raymond_Gimilio3-Apr-08 3:26
Raymond_Gimilio3-Apr-08 3:26 
GeneralRe: A very bad surprise Pin
Dennis Kuppens10-Apr-08 2:39
Dennis Kuppens10-Apr-08 2:39 
GeneralIt helps me to understand the dot-init files management Pin
Raymond_Gimilio2-Apr-08 21:11
Raymond_Gimilio2-Apr-08 21:11 
QuestionNot Unicode Ready? Pin
gunmin11-Feb-07 11:42
gunmin11-Feb-07 11:42 
QuestionThe first and the last time point of data accessibility Pin
Axcell29-Jan-07 1:23
Axcell29-Jan-07 1:23 
AnswerRe: The first and the last time point of data accessibility Pin
Dennis Kuppens31-Jan-07 20:20
Dennis Kuppens31-Jan-07 20:20 
GeneralWhoever Rated This a "1" Should Be Ashamed Pin
Mike O'Neill4-Dec-06 15:04
Mike O'Neill4-Dec-06 15:04 
GeneralRe: Whoever Rated This a "1" Should Be Ashamed Pin
Dennis Kuppens4-Dec-06 22:40
Dennis Kuppens4-Dec-06 22:40 
Hi Mike,

Thank you for your nice gesture to rate my article with a 4 Big Grin | :-D . Your comment that MFC provides this functionality for fee is not completely true. The MFC way is by using functions like GetProfileString and SetProfileString. With my class you do not have to bother about the type of the variable because it uses operator overloading and you do not have to type the long GetProfile functions. In my opinion it does have some handy benefits over the standard MFC way. It looks actually more like the new .NET 2.0 way of handeling application properties.

Greetings,
Dennis.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.