Windows XP Tablet Ed.Portable .NET嵌入式MonoWindows Vista.NET 1.0Windows 2003.NET 1.1.NET 3.0Windows 2000Visual C++ 6.0Windows XP.NET 2.0MFC初学者开发WindowsC++.NET
一个简单的类来读写 INI 文件






3.08/5 (16投票s)
这个类是一个围绕 Win32 API 进行读写 INI 文件的轻量级 MFC 封装。
引言
这是对 Xiangxiong Jian (xiaohe521) 和 Adam (aejw) 在 The Code Project 上开始的 INI 文件类主题的进一步探索。 主要思想基本保持不变:该类是一个围绕 Win32 的轻量级封装。
如果您的应用程序需要同时访问注册表和 INI 文件,或者多个 INI 文件,那么这个类可能很有用。 否则,您可以使用 AfxGetApp()->m_pszProfileName
和 AfxGetApp()->GetProfileXXXX(…)
。
类声明
class CIniFile : public CObject
{
public:
CIniFile(LPCTSTR lpIniFileName, INT iMaxStringLength);
virtual ~CIniFile();
// Attributes
protected:
CString m_strFileName; // path to the INI file
const INT m_MAXSTRLEN; // max length of a string (excluding the key name)
//that can be written/read to/from the
// INI file by this instance
// Operations
public:
CString GetIniFileName();
void SetIniFileName(LPCTSTR lpIniFileName);
BOOL GetStatus(CFileStatus& rStatus);
void GetString(LPCTSTR lpSection, LPCTSTR lpKey,
CString& strRet, LPCTSTR strDefault);
UINT GetInt(LPCTSTR lpSection, LPCTSTR lpKey, INT iDefaultValue);
FLOAT GetFloat(LPCTSTR lpSection, LPCTSTR lpKey, FLOAT fDefaultValue);
BOOL GetStruct(LPCTSTR lpSection, LPCTSTR lpKey,
LPVOID lpRetStruct, UINT iSizeStruct);
void GetSectionNames(CStringList& lstSectionNames);
BOOL WriteSection(LPCTSTR lpSection, LPCTSTR lpData);
BOOL WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpString);
BOOL WriteNumber(LPCTSTR lpSection, LPCTSTR lpKey, INT iValue);
BOOL WriteNumber(LPCTSTR lpSection, LPCTSTR lpKey, FLOAT fValue);
BOOL WriteStruct(LPCTSTR lpSection, LPCTSTR lpKey,
LPVOID lpStruct, UINT iSizeStruct);
BOOL RemoveKey(LPCTSTR lpSection, LPCTSTR lpKey);
};
类实现
实现非常简单。 它基于以下
GetPrivateProfileSectionNames
GetPrivateProfileStruct
GetPrivateProfileString
GetPrivateProfileInt
WritePrivateProfileStruct
WritePrivateProfileString
WritePrivateProfileSection
下载源代码以获取详细信息(注释相当好)。
演示应用程序 / 测试平台
这是演示该类方法的控制台应用程序。 输出到 afxDump
。
// just a dummy structure to write/read to/from the INI file
struct TestStruct : public POINT
{
char charr[8];
};
// PURPOSE: test bed for the INI file class
void TestIniClass()
{
CIniFile iniFile(".\\test1.ini", 1024);
iniFile.SetIniFileName(".\\test2.ini"); // change the file name
// after the class has bee created
// test the writing to the INI file
iniFile.WriteSection("section1", "key1=test1\x000key2=test2"); // make a section
// with 2 new keys
iniFile.WriteSection("section2", "key1=test1"); // make another section
iniFile.WriteString("section1", "key3", "test3"); // create a new key
iniFile.WriteString("section1", "key2", "test4"); // update an existing key
iniFile.WriteString("section1", "key1", NULL); // remove an existing key
iniFile.RemoveKey("section1", "key3"); // remove another existing key
iniFile.WriteNumber("section1", "key4", 123); // write an integer
// to a new key
iniFile.WriteNumber("section1", "key5", -123); // write a negative integer
// to a new key
iniFile.WriteNumber("section1", "key6", -123.456f); // write a float to a new key
TestStruct writeTestStruct;
writeTestStruct.x = 6;
writeTestStruct.y = 7;
strcpy(writeTestStruct.charr, "abcdefg");
iniFile.WriteStruct("section1", "key7", &writeTestStruct, sizeof(TestStruct));
// Test the reading from the INI file
CString str; // string that will receive the output
iniFile.GetString("section1", "key2", str, "default"); TRACE("key2=%s\n", str);
iniFile.GetString("section1", "nokey", str, "default");
TRACE("nokey=%s\n", str); // non-existent key
iniFile.GetString("section3", "key1", str, "default"); TRACE("nokey=%s\n", str);
TRACE("key4=%d\n", iniFile.GetInt("section1", "key4", 0));
TRACE("key5=%d\n", iniFile.GetInt("section1", "key5", 0));
TRACE("key6=%f\n", iniFile.GetFloat("section1", "key6", 0));
TestStruct readTestStruct;
iniFile.GetStruct("section1", "key7", &readTestStruct, sizeof(TestStruct));
TRACE("key7= %ld %ld %s\n", readTestStruct.x,
readTestStruct.y, readTestStruct.charr);
// Test the section list retrieval
CStringList lstSectionNames;
iniFile.GetSectionNames(lstSectionNames);
TRACE("Sections:\n");
for (POSITION pos = lstSectionNames.GetHeadPosition(); pos != NULL; )
{
TRACE("\t%s\n", lstSectionNames.GetNext(pos));
}
}
在这个项目中有什么需要修复、添加或删除的吗? 请告诉我!
参考文献
[1] 一个用于读取 INI 文件的小类
[2] 用于 .NET 的 INI 类
[3] CIni
修订历史
版本 0.1 | 初稿 | Nick Alexeev | 2006年11月24日 |
版本 0.2 | 添加了异常处理 | Nick Alexeev | 2006 年 11 月 30 日 |