请教:在c#中怎么读取ini文件中的值,怎样连接数据库?谢谢! 需要使用kenerl32.dll 系统api,我也不太会,你找找吧。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [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); 可参考以下:C#读写INI文件 虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。 INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value) [Section] Key=Value VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。 INIFILE类: using System;using System.IO;using System.Runtime.InteropServices;//因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices 命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。using System.Text;namespace Ini{ public class IniFile { public string path; //INI文件名 [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文件的API函数 public IniFile(string INIPath) { 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(); } //读取INI文件指定 }} 不一定要用apic#有提供专门的类RegistryKey 和Registry两个类在名字空间Microsoft.Win32中 现在还用这么去读ini文件吗?直接写在webconfig中多好的,多么省事的,偏偏要写在那里边?.net对读取配置文件有很好的支持的.你看看就明白了。 用Ini文件做多语言就是不错的选择 有毛病啊?XML的配置文件Web.Config和app.Config用来干嘛的? 有了委托为什么还要事件 高手进,关于模拟点击<button GDI+ 画多条曲线 SystemInformation.BorderSize怎么得到1? 截取字符串 求助打印报表组件 菜鸟问个小问题,在线等 关于创建Access数据库 请问在VS中任何实现字段合并? 我菜得已经没分了:) 找份工作 怎么更新日期会出错?? 谁知道微软的Money2005的本地版本(下载后使用的)是用什么工具开发的?
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);
C#读写INI文件
虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。
INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value) [Section] Key=Value VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。 INIFILE类: using System;using System.IO;using System.Runtime.InteropServices;//因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices 命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。using System.Text;namespace Ini{ public class IniFile { public string path; //INI文件名 [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文件的API函数 public IniFile(string INIPath) { 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(); } //读取INI文件指定 }}
c#有提供专门的类RegistryKey 和Registry两个类
在名字空间Microsoft.Win32中
.net对读取配置文件有很好的支持的.你看看就明白了。