C#实现利用Windows API读写INI文件的方法
程序员文章站
2023-11-20 22:56:04
本文实例讲述了c#实现利用windows api读写ini文件的方法。分享给大家供大家参考。具体如下:
写入时,如果没有ini文件,自动创建ini
如果在创建时,get...
本文实例讲述了c#实现利用windows api读写ini文件的方法。分享给大家供大家参考。具体如下:
写入时,如果没有ini文件,自动创建ini
如果在创建时,getlasterror:5 检查inipath是否添加了文件名称.ini
using system; using system.collections.generic; using system.text; using system.io; using system.runtime.interopservices; namespace namespace { /// <summary> /// 利用windows api读写ini文件 /// 写入时,如果没有ini文件,自动创建ini /// 如果在创建时,getlasterror:5 检查inipath是否添加了文件名称.ini /// </summary> public class ini { //声明kernel32.dll函数 [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")] public static extern uint getlasterror(); string inipath = null; /// <summary> /// 构造方法 /// </summary> /// <param name="inipath">ini文件的绝对路径,后面不需要斜杠</param> /// <param name="inifilename">ini文件名称使用时不需要斜杠,需要.ini</param> public ini(string inipath,string inifilename) { console.writeline("ini object building"); inipath = inipath + "\\" + inifilename; console.writeline("inifilepath :" + inipath); } /// <summary> /// 写入ini文件 /// </summary> /// <param name="section">section</param> /// <param name="key">key</param> /// <param name="value">value</param> public void iniwritevalue(string section, string key, string value) { console.writeline("---iniwritevalue---"); console.writeline("section :" + section); console.writeline("key :" + key); console.writeline("value :" + value); console.writeline("inipath :" + inipath); uint32 snapshot = getlasterror(); // writeprivateprofilestring(section, key, value, inipath); if (snapshot != getlasterror()) { console.writeline("getlasterror :" + getlasterror()); } } /// <summary> /// 读出ini文件 /// </summary> /// <param name="section">section</param> /// <param name="key">key</param> public string inireadvalue(string section, string key) { stringbuilder result = new stringbuilder(256); getprivateprofilestring(section, key, null, result, 256, inipath); return result.tostring(); } public bool existinifile() { return file.exists(inipath); } /// <summary> /// creat config file to application ini /// </summary> /// <param name="dnf_path"></param> public void createconfig(string ip) { console.writeline("createconfig"); console.writeline("ip:" + ip); try { writeconfigip(ip); if (existinifile()) { console.writeline("配置文件创建成功"); } else { console.writeline("配置文件创建不成功"); } } catch (exception err) { console.writeline("出错信息:" + err.tostring()); } } /// <summary> /// write config for ip information /// </summary> /// <param name="ip"></param> public void writeconfigip(string ip) { string section = "config"; string key = "ip"; string value = ip; try { iniwritevalue(section, key, value); } catch (exception err) { console.writeline("出错信息:" + err.tostring()); } } public string readconfigip() { try { string section = "config"; string result = inireadvalue(section, "ip"); console.writeline("readconfigip result :" + result); return result; } catch (exception err) { console.writeline("出错信息:" + err.tostring()); return "read error"; } } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: axure怎么设计鼠标展开折叠效果?
下一篇: 有什么工作在家可做(在家都可以做的工作)