/********************************** |
首先需要外部配置:1、ini文件所在的文件目录(默认为应用程序下的config目录) |
2、ini文件名 |
注意:ini文件所在的目录是指ini文件所在的文件夹, |
该类只支持创建在当前的应用程序的路径下 |
**********************************/ |
class AFX_EXT_CLASS CMyIniFile |
{ |
public : |
CMyIniFile(CString folderName,CString fileName); |
virtual ~CMyIniFile(); |
void SetFolderName(CString folderName); //设置目录名 |
void SetFileName(CString fileName); //文件名(不包括路径) |
bool WriteStructToFile(__in LPCWSTR lpszSection, //section |
__in LPCWSTR lpszKey, //key |
__in_bcount_opt(uSizeStruct) LPVOID lpStruct, |
__in UINT uSizeStruct); //以结构体的方式写入信息 |
/*以结构体的方式读取信息*/ |
bool GetStruct( __in LPCWSTR lpszSection, //section |
__in LPCWSTR lpszKey, //key |
__out_bcount_opt(uSizeStruct) LPVOID lpStruct, //struct指针 |
__in UINT uSizeStruct /*结构体长度*/ ); |
bool WriteString( LPCTSTR sectionName, |
LPCTSTR lpKeyName, |
LPCTSTR lpString); //写字符串 |
|
private : |
//获取当前程序路径 |
CString _QueryExePath( void ); |
bool CheckFolderExist(CString &strPath); //检测目录是否存在 |
bool BeSureIniFileAvailable( void ); |
bool CreateIniInfoFile(); /*创建ini文件*/ |
|
private : |
CString m_folderName; //ini文件所在的文件夹名称 |
CString m_iniFileName; //ini文件名 |
CString m_appPath; //应用程序的路径 |
CString m_iniFilePathAndName; |
//ini文件属性 |
CString _IniFileName; //表信息配置文件名称(包括地址) |
bool _isHaveInitIniFile; // |
CString _iniSection; |
}; |
//////////////////////////////////////////////////////////////////// |
// CMyIniFile |
CMyIniFile::CMyIniFile(CString folderName,CString fileName) |
{ |
m_folderName = L "config" ; |
m_appPath = _QueryExePath(); |
_isHaveInitIniFile = false ; |
SetFolderName(folderName); |
SetFileName(fileName); |
} |
CMyIniFile::~CMyIniFile() |
{ |
} |
void CMyIniFile::SetFolderName(CString folderName) //设置目录名 |
{ |
m_folderName = folderName; |
} |
void CMyIniFile::SetFileName(CString fileName) //文件名(不包括路径) |
{ |
m_iniFileName = fileName; |
if (m_iniFileName.Find(L ".ini" ) == -1) //文件无后缀名 |
{ |
m_iniFileName+= L ".ini" ; |
} |
} |
// CMyIniFile 成员函数 |
//获取当前程序路径 |
CString CMyIniFile::_QueryExePath() |
{ |
TCHAR path[MAX_PATH]; |
::GetModuleFileName(NULL,path,MAX_PATH); |
CString p(path); |
int nPos = p.ReverseFind( '\\' ); |
ASSERT(-1!=nPos); |
return p.Left(nPos+1); |
} |
bool CMyIniFile::CheckFolderExist(CString &strPath) //检测目录是否存在 |
{ |
WIN32_FIND_DATA wfd; |
bool rValue = false ; |
HANDLE hFind = FindFirstFile(strPath, &wfd); |
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) |
{ |
rValue = true ; |
} |
FindClose(hFind); |
return rValue; |
} |
/*******************************************************/ |
/* 功能: 使用GaugesSetInfo.ini文件,初始化信息 |
/* 正常情况: 如果文件不存在,则新建文件,并初始化 |
/* 如果文件存在且能打开则直接返回true |
/* 不正常情况: 文件存在,但是无法正常打开,则返回false; |
/*******************************************************/ |
bool CMyIniFile::BeSureIniFileAvailable( void ) |
{ |
if (_isHaveInitIniFile == true ) |
{ |
return true ; |
} |
ASSERT(!m_iniFileName.IsEmpty()); |
|
m_iniFilePathAndName.Format(L "%s%s\\%s" ,m_appPath,m_folderName,m_iniFileName); |
|
//ini文件夹是否存在,不存在则创建 |
CString floder; |
floder.Format(L "%s%s" ,m_appPath,m_folderName); |
if (!CheckFolderExist(floder)) |
{ |
_wmkdir(floder); |
} |
//ini文件是否存在,不存在则创建 |
CFileFind finder; |
BOOL bEist = finder.FindFile(m_iniFilePathAndName); |
CFile f; |
CFileException e; |
if (bEist) |
{ //存在 |
if (!f.Open(m_iniFilePathAndName, CFile::modeRead,&e)) |
{ //未被占用 |
#ifdef _DEBUG |
afxDump<< "File could not be opened" <<e.m_cause<< "\n" ; |
return false ; |
#endif |
} |
f.Close(); |
} |
else //不存在 |
{ //重新创建 |
ASSERT(CreateIniInfoFile()); //确保能创建 |
} |
return _isHaveInitIniFile = true ; |
} |
bool CMyIniFile::CreateIniInfoFile() |
{ |
/*创建ini文件*/ |
CFile f; |
CFileException e; |
if (!f.Open(m_iniFilePathAndName, CFile::modeCreate | CFile::modeWrite,&e)) |
{ |
#ifdef _DEBUG |
afxDump<< "File could not be opened" <<e.m_cause<< "\n" ; |
return false ; |
#endif |
} |
f.Close(); |
return true ; |
} |
bool CMyIniFile::GetStruct( __in LPCWSTR lpszSection, |
__in LPCWSTR lpszKey, |
__out_bcount_opt(uSizeStruct) LPVOID lpStruct, |
__in UINT uSizeStruct) //以结构体的方式读取信息 |
{ |
|
BeSureIniFileAvailable(); |
if (!GetPrivateProfileStruct(lpszSection,lpszKey, |
lpStruct, |
uSizeStruct, |
m_iniFilePathAndName)) |
{ //未获取到指定信息 |
return false ; |
} |
return true ; |
|
} |
bool CMyIniFile::WriteString( LPCTSTR sectionName, |
LPCTSTR lpKeyName, |
LPCTSTR lpString) //写字符串 |
{ |
BeSureIniFileAvailable(); |
if (WritePrivateProfileString(sectionName,lpKeyName,lpString,m_iniFilePathAndName)) |
{ |
return true ; |
} |
return false ; |
} |
bool CMyIniFile::WriteStructToFile( __in LPCWSTR lpszSection, |
__in LPCWSTR lpszKey, |
__in_bcount_opt(uSizeStruct) LPVOID lpStruct, |
__in UINT uSizeStruct) //以结构体的方式写入信息 |
{ |
BeSureIniFileAvailable(); |
|
if (!WritePrivateProfileStruct(lpszSection, |
lpszKey, |
lpStruct, |
uSizeStruct, |
m_iniFilePathAndName)) |
{ //未获取到指定信息 |
return false ; |
} |
return true ; |
} |