之前讲了MFC对注册表的操作,虽然现在都提倡把信息写入注册表中,但是有时候需要把配置信息写到本地比如工程目录下面,这样便于查看。
API有两种配置文件的函数。
第一种,无路径的,默认读写的配置文件路径C:\Windows\win.ini (前提是没有调用SetRegistryKey函数,如果调用了此函数,就会把配置文件映射到注册表,相应的数据也会写入到注册表而不是配置文件中)
WriteProfileString(_T("appName"), _T("KeyName"), _T("Value"));
第二种,带路径的,只比第一个多一个参数,其余都一样
WritePrivateProfileString(_T("appName"), _T("KeyName"), _T("Value"),_T("D:\\test.ini"));
下面上代码:.h文件
CString GetPrivateSetting(LPCSTR sKey, LPCSTR sDefault);
int GetPrivateSetting(LPCSTR sKey, int nDefault);
double GetPrivateSetting(LPCSTR sKey, double fDefault, LPCSTR sFormat);
void SetPrivateSetting(LPCSTR sKey, LPCSTR sValue);
void SetPrivateSetting(LPCSTR sKey, int nValue);
void SetPrivateSetting(LPCSTR sKey, double fValue, LPCSTR sFormat);
.cpp文件
#include "stdafx.h"
#include "InitTools.h"
//****************************************************************************************************************
// Function name : GetPrivateSetting
// Description : Get a string from the INI file by sKey.
// Created Date : 2015-5-2
// Last Updated : 2015-5-2
// Author : EasyLiu
//****************************************************************************************************************
CString GetPrivateSetting(LPCSTR sKey, LPCSTR sDefault)
{
CString str;
char buff[100];
GetPrivateProfileString("Settings", sKey, sDefault, buff, sizeof(buff), ".\\app.ini");
if (strcmp(sDefault, buff) == 0) //If the buff is empty
{
WritePrivateProfileString("Settings", sKey, sDefault, ".\\app.ini");//write the default value
}
str = buff;
return str;
}
//****************************************************************************************************************
// Function name : GetPrivateSetting
// Description : Get a integer number from the INI file by sKey.
// Created Date : 2015-5-2
// Last Updated : 2015-5-2
// Author : EasyLiu
//****************************************************************************************************************
int GetPrivateSetting(LPCSTR sKey, int nDefault)
{
int n = GetPrivateProfileInt("Settings", sKey, nDefault, ".\\app.ini");
if (n == nDefault)
{
CString str;
str.Format("%d", nDefault);
//只能写入字符串
WritePrivateProfileString("Settings", sKey, str, ".\\app.ini");
}
return n;
}
//****************************************************************************************************************
// Function name : GetPrivateSetting
// Description : Get a double precision number from the INI file by sKey.
// Created Date : 2015-5-2
// Last Updated : 2015-5-2
// Author : EasyLiu
//****************************************************************************************************************
double GetPrivateSetting(LPCSTR sKey, double fDefault, LPCSTR sFormat)
{
CString str;
str.Format(sFormat, fDefault);
str = GetPrivateSetting(sKey, str);
return atof(str);
}
//****************************************************************************************************************
// Function name : SetPrivateSetting
// Description : Write a string to the INI file by sKey.
// Created Date : 2015-5-2
// Last Updated : 2015-5-2
// Author : EasyLiu
//****************************************************************************************************************
void SetPrivateSetting(LPCSTR sKey, LPCSTR sValue)
{
WritePrivateProfileString("Settings", sKey, sValue, ".\\app.ini");
}
//****************************************************************************************************************
// Function name : SetPrivateSetting
// Description : Write a integer number to the INI file by sKey.
// Created Date : 2015-5-2
// Last Updated : 2015-5-2
// Author : EasyLiu
//****************************************************************************************************************
void SetPrivateSetting(LPCSTR sKey, int nValue)
{
CString str;
str.Format("%d", nValue);
WritePrivateProfileString("Settings", sKey, str, ".\\app.ini");
}
//****************************************************************************************************************
// Function name : SetPrivateSetting
// Description : Write a double precision number to the INI file by sKey.
// Created Date : 2015-5-2
// Last Updated : 2015-5-2
// Author : EasyLiu
//****************************************************************************************************************
void SetPrivateSetting(LPCSTR sKey, double fValue, LPCSTR sFormat)
{
CString str;
str.Format(sFormat, fValue);
WritePrivateProfileString("Settings", sKey, str, ".\\app.ini");
}
这里把配置文件放在了工程目录下面。