c#如何遍历ini文件中Section下所有项,很急,谢谢。

[A]
a=0
b=1
c=200

怎么得到A下的所有项,
谢谢,xml我会,可别人用的是ini,没办法,求救。
你们都没答对。还是自己解决 了。
这样做的。
http://hi.baidu.com/%C9%AB%B1%E9%CC%EC%CF%C2%C3%C0%C5%AE/blog/item/bfe0a696d6fe7d6155fb9698.html

看你最诚实,分给你好了。

c#如何读INI文件中的设置信息

--------------------------------------------------------------------------------

把下面的代码改动一下,就可以在你的程序中使用,当然
别忘记加上名字空间哦。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Sx_Mdi
{

/// <summary>
/// Summary description for Class1.
/// </summary>
public class IniFile
{
//文件INI名称
public string Path;

////声明读写INI文件的API函数
[DllImport("kernel32")]

private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);

//类的构造函数,传递INI文件名
public IniFile(string inipath)
{
//
// TODO: Add constructor logic here
//
Path = inipath;
}

//写INI文件
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.Path);

}

//读取INI文件指定
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.Path);
return temp.ToString();

}

}
}

我复制的

参考资料:不记得了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-06
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace backgroundWA
{
public class INIClass
{
public string inipath;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="INIPath">文件路径</param>
public INIClass(string INIPath)
{
inipath = INIPath;
}
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [TypeName] )</param>
/// <param name="Key">键</param>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值</returns>
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}
}
INIClass ini = new INIClass(@"C:\Program Files\360\360Safe\updatecfg.ini");
//读出文件updatecfg.int中360.dat下的ver的值
string fileIni = ini.IniReadValue("360.dat", "ver");
kernel32.dll里有一个读取Section的函数GetPrivateProfileSection,可惜的是用c#调用的话,不能得到所有key,只能得到第一行,原因他的返回值是"每个字串都用一个NULL分隔,最后一个字串用两个NULL字符中止",c#的话如果遇到null就结束了,导致只能取到第一个key.
第2个回答  2010-04-06
StreamReader sr = new StreamReader("d:\\a.ini",Encoding.Default);
while (sr.Peek() > 0)
{
MessageBox.Show(sr.ReadLine());
}
试试这个看能不能循环弹出每行的值,如果可以稍微改下应该就ok了
第3个回答  2010-04-06
你可以使用下边这个函数读出所有项,然后根据NULL的个数计算各个子项。
DWORD GetPrivateProfileSection(
LPCTSTR lpAppName,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
第4个回答  2019-09-11
你好,请问下你是怎么解决的,我也遇上了这个问题

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网