C#操作INI文件的辅助类IniHelper
程序员文章站
2023-12-19 08:13:58
使用ini配置文件,简单便捷。
该辅助工具类为c#操作ini文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,并对贡献者...
使用ini配置文件,简单便捷。
该辅助工具类为c#操作ini文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,并对贡献者表示感谢。
using system; using system.collections; using system.collections.generic; using system.runtime.interopservices; using system.text; namespace eyuan.common { public static class inihelper { #region 读写ini文件相关 [dllimport("kernel32.dll", entrypoint = "writeprivateprofilestring", charset = charset.ansi)] private static extern long writeprivateprofilestring(string section, string key, string val, string filepath); [dllimport("kernel32.dll", entrypoint = "getprivateprofilestring", charset = charset.ansi)] private static extern int getprivateprofilestring(string section, string key, string def, stringbuilder retval, int size, string filepath); [dllimport("kernel32")] private static extern int getprivateprofileint(string lpapplicationname, string lpkeyname, int ndefault, string lpfilename); [dllimport("kernel32.dll", entrypoint = "getprivateprofilesectionnames", charset = charset.ansi)] private static extern int getprivateprofilesectionnames(intptr lpszreturnbuffer, int nsize, string filepath); [dllimport("kernel32.dll ", entrypoint = "getprivateprofilesection", charset = charset.ansi)] private static extern int getprivateprofilesection(string lpappname, byte[] lpreturnedstring, int nsize, string filepath); #endregion #region 读写操作(字符串) /// <summary> /// 向ini写入数据 /// </summary> /// <param name="section">节点名</param> /// <param name="key">键名</param> /// <param name="value">值(字符串)</param> public static void write(string section, string key, string value, string path) { writeprivateprofilestring(section, key, value, path); } /// <summary> /// 读取ini数据 /// </summary> /// <param name="section">节点名</param> /// <param name="key">键名</param> /// <param name="path">值名</param> /// <returns>值(字符串)</returns> public static string read(string section, string key, string path) { stringbuilder temp = new stringbuilder(255); int i = getprivateprofilestring(section, key, "", temp, 255, path); return temp.tostring(); } #endregion #region 配置节信息 /// <summary> /// 读取一个ini里面所有的节 /// </summary> /// <param name="sections"></param> /// <param name="path"></param> /// <returns>-1:没有节信息,0:正常</returns> public static int getallsectionnames(out string[] sections, string path) { int max_buffer = 32767; intptr preturnedstring = marshal.alloccotaskmem(max_buffer); int bytesreturned = getprivateprofilesectionnames(preturnedstring, max_buffer, path); if (bytesreturned == 0) { sections = null; return -1; } string local = marshal.ptrtostringansi(preturnedstring, (int)bytesreturned).tostring(); marshal.freecotaskmem(preturnedstring); //use of substring below removes terminating null for split sections = local.substring(0, local.length - 1).split('\0'); return 0; } /// <summary> /// 返回指定配置文件下的节名称列表 /// </summary> /// <param name="path"></param> /// <returns></returns> public static list<string> getallsectionnames(string path) { list<string> sectionlist = new list<string>(); int max_buffer = 32767; intptr preturnedstring = marshal.alloccotaskmem(max_buffer); int bytesreturned = getprivateprofilesectionnames(preturnedstring, max_buffer, path); if (bytesreturned != 0) { string local = marshal.ptrtostringansi(preturnedstring, (int)bytesreturned).tostring(); marshal.freecotaskmem(preturnedstring); sectionlist.addrange(local.substring(0, local.length - 1).split('\0')); } return sectionlist; } /// <summary> /// 得到某个节点下面所有的key和value组合 /// </summary> /// <param name="section">指定的节名称</param> /// <param name="keys">key数组</param> /// <param name="values">value数组</param> /// <param name="path">ini文件路径</param> /// <returns></returns> public static int getallkeyvalues(string section, out string[] keys, out string[] values, string path) { byte[] b = new byte[65535];//配置节下的所有信息 getprivateprofilesection(section, b, b.length, path); string s = system.text.encoding.default.getstring(b);//配置信息 string[] tmp = s.split((char)0);//key\value信息 list<string> result = new list<string>(); foreach (string r in tmp) { if (r != string.empty) result.add(r); } keys = new string[result.count]; values = new string[result.count]; for (int i = 0; i < result.count; i++) { string[] item = result[i].split(new char[] { '=' });//key=value格式的配置信息 //value字符串中含有=的处理, //一、value加"",先对""处理 //二、key后续的都为value if (item.length > 2) { keys[i] = item[0].trim(); values[i] = result[i].substring(keys[i].length + 1); } if (item.length == 2)//key=value { keys[i] = item[0].trim(); values[i] = item[1].trim(); } else if (item.length == 1)//key= { keys[i] = item[0].trim(); values[i] = ""; } else if (item.length == 0) { keys[i] = ""; values[i] = ""; } } return 0; } /// <summary> /// 得到某个节点下面所有的key /// </summary> /// <param name="section">指定的节名称</param> /// <param name="keys">key数组</param> /// <param name="path">ini文件路径</param> /// <returns></returns> public static int getallkeys(string section, out string[] keys, string path) { byte[] b = new byte[65535]; getprivateprofilesection(section, b, b.length, path); string s = system.text.encoding.default.getstring(b); string[] tmp = s.split((char)0); arraylist result = new arraylist(); foreach (string r in tmp) { if (r != string.empty) result.add(r); } keys = new string[result.count]; for (int i = 0; i < result.count; i++) { string[] item = result[i].tostring().split(new char[] { '=' }); if (item.length == 2) { keys[i] = item[0].trim(); } else if (item.length == 1) { keys[i] = item[0].trim(); } else if (item.length == 0) { keys[i] = ""; } } return 0; } /// <summary> /// 获取指定节下的key列表 /// </summary> /// <param name="section">指定的节名称</param> /// <param name="path">配置文件名称</param> /// <returns>key列表</returns> public static list<string> getallkeys(string section, string path) { list<string> keylist = new list<string>(); byte[] b = new byte[65535]; getprivateprofilesection(section, b, b.length, path); string s = system.text.encoding.default.getstring(b); string[] tmp = s.split((char)0); list<string> result = new list<string>(); foreach (string r in tmp) { if (r != string.empty) result.add(r); } for (int i = 0; i < result.count; i++) { string[] item = result[i].split(new char[] { '=' }); if (item.length == 2 || item.length == 1) { keylist.add(item[0].trim()); } else if (item.length == 0) { keylist.add(string.empty); } } return keylist; } /// <summary> /// 获取值 /// </summary> /// <param name="section"></param> /// <param name="path"></param> /// <returns></returns> public static list<string> getallvalues(string section, string path) { list<string> keylist = new list<string>(); byte[] b = new byte[65535]; getprivateprofilesection(section, b, b.length, path); string s = system.text.encoding.default.getstring(b); string[] tmp = s.split((char)0); list<string> result = new list<string>(); foreach (string r in tmp) { if (r != string.empty) result.add(r); } for (int i = 0; i < result.count; i++) { string[] item = result[i].split(new char[] { '=' }); if (item.length == 2 || item.length == 1) { keylist.add(item[1].trim()); } else if (item.length == 0) { keylist.add(string.empty); } } return keylist; } #endregion #region 通过值查找键(一个节中的键唯一,可能存在多个键值相同,因此反查的结果可能为多个) /// <summary> /// 第一个键 /// </summary> /// <param name="section"></param> /// <param name="path"></param> /// <param name="value"></param> /// <returns></returns> public static string getfirstkeybyvalue(string section, string path, string value) { foreach (string key in getallkeys(section, path)) { if (readstring(section, key, "", path) == value) { return key; } } return string.empty; } /// <summary> /// 所有键 /// </summary> /// <param name="section"></param> /// <param name="path"></param> /// <param name="value"></param> /// <returns></returns> public static list<string> getkeysbyvalue(string section, string path, string value) { list<string > keys = new list<string>(); foreach (string key in getallkeys(section, path)) { if (readstring(section, key, "", path) == value) { keys.add(key); } } return keys; } #endregion #region 具体类型的读写 #region string /// <summary> /// /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="defaultvalue" /> /// <param name="path"></param> /// <returns></returns> public static string readstring(string sectionname, string keyname, string defaultvalue, string path) { const int maxsize = 255; stringbuilder temp = new stringbuilder(maxsize); getprivateprofilestring(sectionname, keyname, defaultvalue, temp, 255, path); return temp.tostring(); } /// <summary> /// /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="value"></param> /// <param name="path"></param> public static void writestring(string sectionname, string keyname, string value, string path) { writeprivateprofilestring(sectionname, keyname, value, path); } #endregion #region int /// <summary> /// /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="defaultvalue"></param> /// <param name="path"></param> /// <returns></returns> public static int readinteger(string sectionname, string keyname, int defaultvalue, string path) { return getprivateprofileint(sectionname, keyname, defaultvalue, path); } /// <summary> /// /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="value"></param> /// <param name="path"></param> public static void writeinteger(string sectionname, string keyname, int value, string path) { writeprivateprofilestring(sectionname, keyname, value.tostring(), path); } #endregion #region bool /// <summary> /// 读取布尔值 /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="defaultvalue"></param> /// <param name="path"></param> /// <returns></returns> public static bool readboolean(string sectionname, string keyname, bool defaultvalue, string path) { int temp = defaultvalue ? 1 : 0; int result = getprivateprofileint(sectionname, keyname, temp, path); return (result == 0 ? false : true); } /// <summary> /// 写入布尔值 /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="value"></param> /// <param name="path"></param> public static void writeboolean(string sectionname, string keyname, bool value, string path) { string temp = value ? "1 " : "0 "; writeprivateprofilestring(sectionname, keyname, temp, path); } #endregion #endregion #region 删除操作 /// <summary> /// 删除指定项 /// </summary> /// <param name="sectionname"></param> /// <param name="keyname"></param> /// <param name="path"></param> public static void deletekey(string sectionname, string keyname, string path) { writeprivateprofilestring(sectionname, keyname, null, path); } /// <summary> /// 删除指定节下的所有项 /// </summary> /// <param name="sectionname"></param> /// <param name="path"></param> public static void erasesection(string sectionname, string path) { writeprivateprofilestring(sectionname, null, null, path); } #endregion #region 判断节、键是否存在 /// <summary> /// 指定节知否存在 /// </summary> /// <param name="section"></param> /// <param name="filename"></param> /// <returns></returns> public static bool existsection(string section, string filename) { string[] sections = null; getallsectionnames(out sections, filename); if (sections != null) { foreach (var s in sections) { if (s == section) { return true; } } } return false; } /// <summary> /// 指定节下的键是否存在 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="filename"></param> /// <returns></returns> public static bool existkey(string section, string key, string filename) { string[] keys = null; getallkeys(section, out keys, filename); if (keys != null) { foreach (var s in keys) { if (s == key) { return true; } } } return false; } #endregion #region 同一section下添加多个key\value /// <summary> /// /// </summary> /// <param name="section"></param> /// <param name="keylist"></param> /// <param name="valuelist"></param> /// <param name="path"></param> /// <returns></returns> public static bool addsectionwithkeyvalues(string section, list<string> keylist, list<string> valuelist, string path) { bool brst = true; //判断section是否已经存在,如果存在,返回false //已经存在,则更新 //if (getallsectionnames(path).contains(section)) //{ // return false; //} //判断keylist中是否有相同的key,如果有,返回false //添加配置信息 for (int i = 0; i < keylist.count; i++) { writestring(section, keylist[i], valuelist[i], path); } return brst; } #endregion } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。