c#读取ini文件读取节名字
(2012-01-05 16:51:26)
标签:
ciniit |
分类: ASP.NET |
|
GetProfileInt |
从win.ini文件指定Section中读取一个int属性值 |
|
GetProfileSection |
从win.ini文件指定Section中获取所有的属性 |
|
GetProfileString |
从win.ini文件指定Section中读取一个文本属性值 |
|
WriteProfileSection |
向win.ini文件写入一个Section |
|
WriteProfileString |
向win.ini文件指定Section中写入一个文本属性值 |
|
GetPrivateProfileInt |
从指定的INI文件的Section中读取一个int属性值 |
|
GetPrivateProfileSection |
从指定的INI文件中读取指定的Section |
|
GetPrivateProfileSection |
从指定的INI文件中获取所有的Section名 |
|
GetPrivateProfileString |
从指定的INI文件的Section中读取一个文本属性值 |
|
GetPrivateProfileStruct |
从指定的INI文件的Section中读取一个结构属性值 |
|
WritePrivateProfileSecti |
向指定的INI文件写入一个Section |
|
WritePrivateProfileStrin |
向指定的INI文件的Section中写入一个文本属性值 |
|
WritePrivateProfileStruc |
向指定的INI文件的Section中写入一个结构属性值 |
1、C#操作INI的方法一般是采用调用DLL中的INI文件的函数
#region 导入DLL函数
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileString(string segName,
string keyName, string sDefault, StringBuilder buffer, int nSize,
string fileName);
public extern static int GetPrivateProfileStringA
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSection
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileSecti
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileStrin
[DllImport("kernel32.dll")]
public extern static int
GetPrivateProfileSection
#endregion
2、 INI文件的fileName必须使用绝对路径,说明如下
If the lpFileName parameter does not contain a full path and
file name for the file,
WritePrivateProfileStrin
If the file does not exist,this function creates the file in the
Windows directory.
3、封装的方法中,最有价值的是获取所有Sections和所有的Keys,网上关于这个的代码大部分是错误的,这里给出一个正确的方法:
/// 返回该配置文件中所有Section名称的集合
public ArrayList ReadSections()
}
// 获取节点的所有KEY值
public ArrayList ReadKeys(string sectionName) {
}
4、批判网上一个常见的代码范例
public string ReadString(string Section, string Key) {
}
用StringBuilder只能读出第一行,不是一个好的写法,正确的做法应该是用char[],因为返回的是一个二进制的串,字符串之间是用"\0"分隔的,具体的见我前面的代码。

加载中…