C#怎么读取和写入INI的配置信息? 能举个例子吗?比如我想读取HELLO.INI中的HELLO=你好怎么读取[HELLO]HELLO=你好在比如怎么写入配置项?把你好改成你好吗还有怎么读取TXT中的内容? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 又是文件读写很简单1,打开MSDN2,在索引里输入System.IO3,自己边看边学边实践 [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(); } 方法有很多,可以调用系统的API如2楼,也可以用C#自己的文件读写类,看你习惯用哪个了。 2楼的这是标准的微软INI文件读取方式。也可以自己做文本读写的类,判断“[]”标签即可,很容易的。 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"); 困恼小久的问题,高版本informix SDK连接数据库报错问题。 高手咨询:关于数据库表设计 求一段函数? CLI && c# 怪异问题 求助好心人!如何在.NET中导出用户自定义控件,我想用在.NET中的其它工程中 关于C#中copy目录 推荐 .Net的通讯组件Eastasp.Framework.Net 怎样获得类的默认属性值? 要比较两个位图中的文字是否相同。急急急急,解决即刻加分。每妹先谢过了。 我能不能用手机发一条短信,用我的电脑受到呢? 世界时间 C#如何将textbox对象定义为静态
2,在索引里输入System.IO
3,自己边看边学边实践
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();
}
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");