C#实现读写ini文件类实例
程序员文章站
2023-12-05 22:25:04
本文实例讲述了c#实现读写ini文件类。分享给大家供大家参考。具体如下:
这个c#类封装了对ini配置文件进行操作所需的各种函数,包括读取键值、读取键值、删除段落等...
本文实例讲述了c#实现读写ini文件类。分享给大家供大家参考。具体如下:
这个c#类封装了对ini配置文件进行操作所需的各种函数,包括读取键值、读取键值、删除段落等
using system; using system.runtime.interopservices; using system.text; namespace dotnet.utilities { /// <summary> /// ini文件读写类。 /// </summary> public class inifile { public string path; public inifile(string inipath) { path = inipath; } [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); [dllimport("kernel32")] private static extern int getprivateprofilestring(string section, string key, string defval, byte[] retval, int size, string filepath); /// <summary> /// 写ini文件 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="value"></param> public void iniwritevalue(string section,string key,string value) { writeprivateprofilestring(section,key,value,this.path); } /// <summary> /// 读取ini文件 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <returns></returns> public string inireadvalue(string section,string key) { stringbuilder temp = new stringbuilder(255); int i = getprivateprofilestring(section,key,"",temp, 255, this.path); return temp.tostring(); } public byte[] inireadvalues(string section, string key) { byte[] temp = new byte[255]; int i = getprivateprofilestring(section, key, "", temp, 255, this.path); return temp; } /// <summary> /// 删除ini文件下所有段落 /// </summary> public void clearallsection() { iniwritevalue(null,null,null); } /// <summary> /// 删除ini文件下personal段落下的所有键 /// </summary> /// <param name="section"></param> public void clearsection(string section) { iniwritevalue(section,null,null); } } }
希望本文所述对大家的c#程序设计有所帮助。
下一篇: C#实现根据年份计算生肖属相的方法