头文件
stdfax.h
#pragma once
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 从 Windows 头文件中排除极少使用的信息
#endif
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的
#define _ATL_NO_AUTOMATIC_NAMESPACE // 避免类名称冲突
#include <afxctl.h> // ActiveX 控件的 MFC 支持
#include <afxext.h> // MFC 扩展
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h> // MFC 对 Internet Explorer 4 公共控件的支持
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC 对 Windows 公共控件的支持
#endif // _AFX_NO_AFXCMN_SUPPORT
// 如果不希望使用 MFC 数据库类,
// 请删除下面的两个包含文件
#ifndef _WIN64
#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h> // MFC ODBC 数据库类
#endif // _AFX_NO_DB_SUPPORT
#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h> // MFC DAO 数据库类
#endif // _AFX_NO_DAO_SUPPORT
#endif // _WIN64
ConfigFile类的声明文件
#pragma once
#include "stdafx.h"
class ConfigFile
{
public:
ConfigFile(void);
static CString GetIniString(CString iniFilePath,CString app,CString key);//获取配置文件的string 值
static int GetIniInt(CString iniFilePath,CString app,CString key);//获取配置文件中int值
static void SetIniString(CString iniFilePath,CString app,CString key,CString strData);//设置配置文件的路径(包括文件名ini文件) 配置的属性 配置的关键字 配置的详细内容
~ConfigFile(void);
};
ConfigFile类的实现文件
#include "ConfigFile.h"
ConfigFile::ConfigFile(void)
{
}
CString ConfigFile::GetIniString( CString sIniFilePath, CString app, CString key)
{
char tempChar[1024] = {0};
GetPrivateProfileString(app, key, _T(""), tempChar, 1024, sIniFilePath );
CString temp;
temp.Format( _T("%s"), tempChar );
return temp;
}
int ConfigFile::GetIniInt(CString sIniFilePath, CString app, CString key)
{
return GetPrivateProfileInt( app, key, 0, sIniFilePath );
}
void ConfigFile::SetIniString( CString sIniFilePath, CString app, CString key, CString strData )
{
WritePrivateProfileString( app, key, strData, sIniFilePath );
}
ConfigFile::~ConfigFile(void)
{
}
测试配置类的main函数
#include<iostream>
#include "ConfigFile.h"
int main()
{
CFileFind find;//查找文件类
BOOL isFind = find.FindFile("D:\\test.ini");
if (!isFind)
{
ConfigFile::SetIniString("D:\\test.ini","app","key","gac");
ConfigFile::SetIniString("D:\\test.ini","app1","key","gac");
ConfigFile::SetIniString("D:\\test.ini","app","key1","gac1");
}
getchar();
return 0;
}