首页 > 开发 > 综合 > 正文

用C#读写ini文件

2024-07-21 02:18:15
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。可以通过调用kernel32.dll中的两个api:writeprivateprofilestring,getprivateprofilestring来实现对ini 文件的读些。

具体实现的代码如下:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
using system.text;


namespace iniprocess
{

public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.button button2;
private system.windows.forms.button button1;

[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 void iniwritevalue(string section,string key,string value,string filepath)//对ini文件进行写操作的函数
{
writeprivateprofilestring(section,key,value,filepath);
}

public string inireadvalue(string section,string key,string filepath)//对ini文件进行读操作的函数
{
stringbuilder temp = new stringbuilder(255);
int i = getprivateprofilestring(section,key,"",temp,
255, filepath);
return temp.tostring();

}




private void button1_click(object sender, system.eventargs e)
{

this.textbox1 .text= inireadvalue("odbc 32 bit data sources","ms access database","e://temp//odbc.ini");

}

private void button2_click(object sender, system.eventargs e)
{

iniwritevalue ("odbc 32 bit data sources","ms access database",this.textbox1 .text,"e://temp//odbc.ini");
}
}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表