加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

c#读取ini文件读取节名字

(2012-01-05 16:51:26)
标签:

c

ini

it

分类: 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

GetPrivateProfileSectionNames

从指定的INI文件中获取所有的Section

GetPrivateProfileString

从指定的INI文件的Section中读取一个文本属性值

GetPrivateProfileStruct

从指定的INI文件的Section中读取一个结构属性值

WritePrivateProfileSection

向指定的INI文件写入一个Section

WritePrivateProfileString

向指定的INI文件的Section中写入一个文本属性值

WritePrivateProfileStruct

向指定的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(string segName, string keyName, string sDefault, byte[] buffer, int iLen, string fileName); // ANSI版本

[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);

[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName);

[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName);

[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);

#endregion

2、 INI文件的fileName必须使用绝对路径,说明如下

If the lpFileName parameter does not contain a full path and file name for the file,
WritePrivateProfileString searches the Windows directory for the file.
If the file does not exist,this function creates the file in the Windows directory.

3、封装的方法中,最有价值的是获取所有Sections和所有的Keys,网上关于这个的代码大部分是错误的,这里给出一个正确的方法:

/// 返回该配置文件中所有Section名称的集合

public ArrayList ReadSections() {
byte[] buffer = new byte[65535];
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), _FileName);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel>0) {
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++) {
if (buffer[iCnt] == 0x00) {
tmp =System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt-iPos ).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}

// 获取节点的所有KEY值

public ArrayList ReadKeys(string sectionName) {

byte[] buffer = new byte[5120];
int rel = GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), _FileName);

int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0) {
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++) {
if (buffer[iCnt] == 0x00) {
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt-iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}

4、批判网上一个常见的代码范例

public string ReadString(string Section, string Key) {
StringBuilder buffer= new StringBuilder(65535);
GetPrivateProfileString(Section, Key, "", buffer, buffer.Capacity, _FileName);
return buffer.ToString();
}

用StringBuilder只能读出第一行,不是一个好的写法,正确的做法应该是用char[],因为返回的是一个二进制的串,字符串之间是用"\0"分隔的,具体的见我前面的代码。

 GetString("Section", "Key");

 public string GetString(string Section, string Key)
    {
        string basedirectory = System.AppDomain.CurrentDomain.BaseDirectory;
        string path = Path.Combine(basedirectory, "vehstat.ini");
        StringBuilder temp = new StringBuilder(500);
        int i = GetPrivateProfileString(Section, Key, "", temp, 500, path);
        return temp.ToString();
    }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有