// IniFile.cpp: implementation of the CIniReader class. #include "stdafx.h" #include "IniReader.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif // 用于返回指定段名的键值; CString CIniReader::GetKeyValueText(CString strKey,CString strSection) { char ac_Result[255]; // Get the info from the .ini file m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey, "",ac_Result, 255, (LPCTSTR)m_strFileName); CString strResult(ac_Result); return strResult; } // 用于返回指定段名的键值; int CIniReader::GetKeyValueInt(CString strKey,CString strSection) { char ac_Result[255]; // Get the info from the .ini file m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey, "",ac_Result, 255, (LPCTSTR)m_strFileName); int nRet = atoi(ac_Result); return nRet; } // 用于添加或设置指定段名的键值; long CIniReader::SetKey(CString strValue, CString strKey, CString strSection) { m_lRetValue = WritePrivateProfileString (strSection, strKey, strValue, m_strFileName); return m_lRetValue; } // 用于判断一个段名是否存在; BOOL CIniReader::SectionExists(CString strSection) { char ac_Result[100]; CString csAux; // Get the info from the .ini file m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL, "",ac_Result, 90, (LPCTSTR)m_strFileName); // Return if we could retrieve any info from that section return (m_lRetValue > 0); } // 用于返回ini文件中的所有段名; CStringList* CIniReader::GetSectionNames() //returns collection of section names { char ac_Result[2000]; m_sectionList->RemoveAll(); m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName); CString strSectionName; for(int i=0; iInsertAfter(m_sectionList->GetTailPosition(),strSectionName); } strSectionName = ""; } } return m_sectionList; } // 用于返回ini文件所有段名的数据内容 ; CStringList* CIniReader::GetSectionData(CString strSection) { char ac_Result[2000]; //change size depending on needs m_sectionDataList->RemoveAll(); m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName); CString strSectionData; for(int i=0; iInsertAfter(m_sectionDataList->GetTailPosition(),strSectionData); } strSectionData = ""; } } return m_sectionDataList; } // 设置Ini文件名; void CIniReader::SetINIFileName(CString strINIFile) { m_strFileName = strINIFile; } int CIniReader::GetSecCount() { return m_sectionList->GetSize(); } int CIniReader::GetKeyCount(/*CString strSec*/) { return m_sectionDataList->GetSize(); }