C#中如何读、写INI配置文件?

alf 2003-10-24 11:00:42
如题!
...全文
96 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuji 2003-10-24
  • 打赏
  • 举报
回复
C#读写INI文件:
http://www.zdnet.com.cn/developer/code/story/0,2000081534,39065664,00.htm

http://developer.ccidnet.com/pub/article/c1137_a26002_p1.html
bGene 2003-10-24
  • 打赏
  • 举报
回复
加载命名空间:
using System.Text;
using System.Runtime.InteropServices;


调用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文件
string FilePath = Application.StartupPath + "Test.ini";
string Section = "Section"; //段落名
string Key = "Key"; //节点名
string KeyValue = "KeyValue"; //节点值
long temp = WritePrivateProfileString(Section,Key,KeyValue,FilePath);
if(temp>0)
MessageBox.Show("成功写入INI文件!","信息");


读INI文件
StringBuilder Temp = new StringBuilder(255);
string FilePath = Application.StartupPath + "Test.ini";
string Section = "Section"; //段落名
string Key = "Key"; //节点名
int i = GetPrivateProfileString ( Section,Key,"无法读取对应数值!",Temp,255,FilePath);
//显示读取的数值
MessageBox.Show(Temp.ToString());

110,561

社区成员

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

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

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