C#怎么读取和写入INI的配置信息?

hungpo 2010-12-02 02:19:39
能举个例子吗?
比如我想读取HELLO.INI中的HELLO=你好怎么读取
[HELLO]
HELLO=你好
在比如怎么写入配置项?把你好改成你好吗


还有怎么读取TXT中的内容?
...全文
499 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
guo_qiang929 2010-12-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 robbish 的回复:]
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

public class IniFile
{
//文件INI名称
//public string Path;

/**/////声明读写INI文件的API函……
[/Quote]


楼上的给出答案了
robbish 2010-12-02
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

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,string Path)
{
WritePrivateProfileString(Section, Key, Value,Path);
}

/// <summary>
/// 读取INI文件指定的文件数据
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Path"></param>
/// <returns></returns>
public string IniReadValue(string Section, string Key,string Path)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, Path);
return temp.ToString();
}
/**//// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns>布尔值 </returns>
//public bool ExistINIFile()
//{
// return File.Exists(this.Path);
//}

}

//调用测试

向INI文件写入数据

IniWriteValue("Login Information","Password ","73C18C59A39B3","F:\test.ini");

查看INI文件信息

[Login Information]
Password=73C18C59A39B3

//读INI文件里的Password值

String Password = IniFile.IniReadValue("User Information","Password","F:\test.ini");
cl98 2010-12-02
  • 打赏
  • 举报
回复
2楼的这是标准的微软INI文件读取方式。也可以自己做文本读写的类,判断“[]”标签即可,很容易的。
biancheng1000 2010-12-02
  • 打赏
  • 举报
回复
方法有很多,可以调用系统的API如2楼,也可以用C#自己的文件读写类,看你习惯用哪个了。
xxiju203 2010-12-02
  • 打赏
  • 举报
回复
[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);//读配置文件
public static void writeIni(string section, string key, string value, string filePath)//写配置文件
{
WritePrivateProfileString(section, key, value, filePath);
}
public static string readIni(string section, string key, string filePath)//读配置文件
{
StringBuilder tempBuilder = new StringBuilder(1024);
int i = GetPrivateProfileString(section, key, "", tempBuilder, 1024, filePath);
return tempBuilder.ToString();
}
Return门徒 2010-12-02
  • 打赏
  • 举报
回复
又是文件读写

很简单

1,打开MSDN
2,在索引里输入System.IO
3,自己边看边学边实践

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧