65.9K
CodeProject 正在变化。 阅读更多。
Home

完整的 Win32 INI 文件实用程序类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (56投票s)

2007年8月16日

CPOL

3分钟阅读

viewsIcon

176587

downloadIcon

8641

Win32 INI API 的完整包装器

引言

虽然微软不再建议使用这种方法来存储数据,但由于遗留原因,许多应用程序仍然需要读取和写入 INI 文件。Win32 提供了几种处理 INI 文件的方法,但这些方法没有被 .NET Framework 公开。本文介绍了一个简单的类,允许托管应用程序与 INI 文件交互。

背景

INI 文件是一个特殊格式的文本文件,包含一个或多个节。每个节可以有一个或多个设置(或键)。下面显示了一个示例文件

.
[section1]
key1=1
key2=string1
.
.
.

Using the Code

附件的 IniFile.cs 文件包含 IniFile 类的所有代码。您可以通过将要读取和/或写入的 INI 文件的路径传递给构造函数来创建此类的新实例。

//Create a new IniFile object.
IniFile iniFile = new IniFile(@"c:\boot.ini");

从 INI 文件读取

System.Data.DataReader.IDataReader 接口类似,IniFile 类提供了几种从 INI 文件读取不同类型数据的方法。这些方法中的每一个都采用 "GetX" 的形式,其中 X 是返回值所请求的数据类型。例如,要从您的 INI 文件中读取一个 System.Int32 值,您可以使用 GetInt32 方法。

//Read an Int16 value from an INI file.
short value1 = iniFile.GetInt16("section1", "key1", 0);

//Read an Int32 value from an INI file.
int value2 = iniFile.GetInt32("section1", "key2", 0);

//Read a String from an INI file.
string value3 = iniFile.GetString("section1", "key3", "default value");

//Read an Double value from an INI file.
double value4 = iniFile.GetDouble("section1", "key4", 0.0);

//Read an Boolean value from an INI file.
bool value5 = iniFile.GetBoolean(quot;section1", "key5", true);

如上所示,这些方法中的每一个都接受三个参数。第一个参数指定要从中读取的节的名称。第二个参数指定要从中读取的特定键。最后一个参数指定如果未找到节/键则要返回的值。请注意,字符串值限制为 32KB 或更小。尝试读取长度超过 32KB 的设置将导致数据被截断为 32KB。

IniFile 类还提供了枚举文件中节或键的列表的方法。这些方法中的每一个都返回一个字符串数组。

//Get the list of sections in the INI file.
string [] sectionNames = iniFile.GetSectionNames();

//Get the list of keys in the first section.
string [] keyNames = iniFile.GetKeyNames(sectionNames[0]);

最后,IniFile 类提供了允许调用者一次读取给定节中所有键的方法。此功能由两个函数提供:GetSectionValuesGetSectionValuesAsList

GetSectionValues 是这两个方法中更方便的一个。它将节中的所有键/值对作为 Dictionary<string, string> 对象返回。这使得一次获取几个属性的值变得非常容易。

//Get the key/value pairs in a section of the INI file as a Dictionary.

//Get the key/values in section1.
Dictionary<string, string> sectionValues = iniFile.GetSectionValues("section1");

string value1 = sectionValues["key1"];
string value2 = sectionValues["key2"];

如果一个节包含多个具有相同键的值,则 GetSectionValues 始终返回文件中的第一个值。如果您需要能够获取节中的所有值,而不管键名是什么,则可以使用 GetSectionValuesAsList 方法。

//Get the key/value pairs in a section of the INI file as a List.

//Get the key/values in section1.
List<KeyValuePair<string, string>> sectionValues;

sectionValues = iniFile.GetSectionValuesAsList("section1");

foreach (KeyValuePair<string, string> pair in sectionValues)
{
    //Process the keys here...
}

这两种方法与 GetString 一样,共享相同的 32KB 限制。如果您的节可以包含超过 32KB 的数据,请分别读取每个设置。

写入 INI 文件

IniFile 类还提供了写入 INI 文件的方法。WriteValue 方法的各种重载允许调用者将字符串、整数或双精度值写入 INI 文件。这些函数中的每一个都接受三个参数。第一个参数指定要写入的节的名称。第二个参数指定要写入的特定键。最后一个参数指定键的新值。如果节和键已经存在,则现有值将被覆盖。如果指定的节或键不存在,则它们会自动创建。

//Write a string to the iniFile
iniFile.WriteValue("section1", "key2", "Test value");

//Write a Int32 to the iniFile.
iniFile.WriteValue("section1", "key1", 128);

//Write a Double to the iniFile.
iniFile.WriteValue("section1", "key1", 42.8);

//Write a Boolean to the iniFile.
iniFile.WriteValue("section1", "key1", true);

从 INI 文件中删除数据

IniFile 类提供了从 INI 文件中删除键和节的方法。使用 DeleteKey 删除特定的键,使用 DeleteSection 删除 INI 文件中的整个节。

//Delete key2 from section1
iniFile.DeleteKey("section1", "key2");

//Delete section2 (including all keys)
iniFile.DeleteSection("section2");

历史

  • 05/28/2014
    • 更正了 GetInt16 的返回类型
    • 添加了 GetBoolean 和新的 WriteValue 方法以写入布尔值
    • 如果键的文本值无法解析,GetDouble 不再抛出异常。它现在在这种情况下返回默认值。
  • 11/18/2013
    • 修复了注释中的一些拼写错误。添加了关于构造函数可能抛出的异常的文档。
  • 11/19/2007
    • 添加了参数检查
    • 添加了 DeleteKey DeleteSection
    • 添加了关于大多数方法 32KB 限制的说明
  • 11/19/2007
    • 修复了 XML 注释
    • 添加了 GetSectionValues GetSectionValuesAsList
  • 2007 年 8 月 14 日 - 初始版本
© . All rights reserved.