INIHelper.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace ProductionLineMonitor.Core.Utils
  7. {
  8. public static class INIHelper
  9. {
  10. #region 读写INI文件相关
  11. [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", CharSet = CharSet.Ansi)]
  12. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  13. [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", CharSet = CharSet.Ansi)]
  14. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  15. [DllImport("kernel32")]
  16. private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);
  17. [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
  18. private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
  19. [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
  20. private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
  21. #endregion
  22. #region 读写操作(字符串)
  23. /// <summary>
  24. /// 向INI写入数据
  25. /// </summary>
  26. /// <PARAM name="Section">节点名</PARAM>
  27. /// <PARAM name="Key">键名</PARAM>
  28. /// <PARAM name="Value">值(字符串)</PARAM>
  29. public static void Write(string Section, string Key, string Value, string path)
  30. {
  31. WritePrivateProfileString(Section, Key, Value, path);
  32. }
  33. /// <summary>
  34. /// 读取INI数据
  35. /// </summary>
  36. /// <PARAM name="Section">节点名</PARAM>
  37. /// <PARAM name="Key">键名</PARAM>
  38. /// <PARAM name="Path">值名</PARAM>
  39. /// <returns>值(字符串)</returns>
  40. public static string Read(string Section, string Key, string path)
  41. {
  42. StringBuilder temp = new StringBuilder(255);
  43. int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
  44. return temp.ToString();
  45. }
  46. #endregion
  47. #region 配置节信息
  48. /// <summary>
  49. /// 读取一个ini里面所有的节
  50. /// </summary>
  51. /// <param name="sections"></param>
  52. /// <param name="path"></param>
  53. /// <returns>-1:没有节信息,0:正常</returns>
  54. public static int GetAllSectionNames(out string[] sections, string path)
  55. {
  56. int MAX_BUFFER = 32767;
  57. IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
  58. int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
  59. if (bytesReturned == 0)
  60. {
  61. sections = null;
  62. return -1;
  63. }
  64. string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
  65. Marshal.FreeCoTaskMem(pReturnedString);
  66. //use of Substring below removes terminating null for split
  67. sections = local.Substring(0, local.Length - 1).Split('\0');
  68. return 0;
  69. }
  70. /// <summary>
  71. /// 返回指定配置文件下的节名称列表
  72. /// </summary>
  73. /// <param name="path"></param>
  74. /// <returns></returns>
  75. public static List<string> GetAllSectionNames(string path)
  76. {
  77. List<string> sectionList = new List<string>();
  78. int MAX_BUFFER = 32767;
  79. IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
  80. int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
  81. if (bytesReturned != 0)
  82. {
  83. string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
  84. Marshal.FreeCoTaskMem(pReturnedString);
  85. sectionList.AddRange(local.Substring(0, local.Length - 1).Split('\0'));
  86. }
  87. return sectionList;
  88. }
  89. /// <summary>
  90. /// 得到某个节点下面所有的key和value组合
  91. /// </summary>
  92. /// <param name="section">指定的节名称</param>
  93. /// <param name="keys">Key数组</param>
  94. /// <param name="values">Value数组</param>
  95. /// <param name="path">INI文件路径</param>
  96. /// <returns></returns>
  97. public static int GetAllKeyValues(string section, out string[] keys, out string[] values, string path)
  98. {
  99. byte[] b = new byte[65535];//配置节下的所有信息
  100. GetPrivateProfileSection(section, b, b.Length, path);
  101. string s = System.Text.Encoding.Default.GetString(b);//配置信息
  102. string[] tmp = s.Split((char)0);//Key\Value信息
  103. List<string> result = new List<string>();
  104. foreach (string r in tmp)
  105. {
  106. if (r != string.Empty)
  107. result.Add(r);
  108. }
  109. keys = new string[result.Count];
  110. values = new string[result.Count];
  111. for (int i = 0; i < result.Count; i++)
  112. {
  113. string[] item = result[i].Split(new char[] { '=' });//Key=Value格式的配置信息
  114. //Value字符串中含有=的处理,
  115. //一、Value加"",先对""处理
  116. //二、Key后续的都为Value
  117. if (item.Length > 2)
  118. {
  119. keys[i] = item[0].Trim();
  120. values[i] = result[i].Substring(keys[i].Length + 1);
  121. }
  122. if (item.Length == 2)//Key=Value
  123. {
  124. keys[i] = item[0].Trim();
  125. values[i] = item[1].Trim();
  126. }
  127. else if (item.Length == 1)//Key=
  128. {
  129. keys[i] = item[0].Trim();
  130. values[i] = "";
  131. }
  132. else if (item.Length == 0)
  133. {
  134. keys[i] = "";
  135. values[i] = "";
  136. }
  137. }
  138. return 0;
  139. }
  140. /// <summary>
  141. /// 得到某个节点下面所有的key
  142. /// </summary>
  143. /// <param name="section">指定的节名称</param>
  144. /// <param name="keys">Key数组</param>
  145. /// <param name="path">INI文件路径</param>
  146. /// <returns></returns>
  147. public static int GetAllKeys(string section, out string[] keys, string path)
  148. {
  149. byte[] b = new byte[65535];
  150. GetPrivateProfileSection(section, b, b.Length, path);
  151. string s = System.Text.Encoding.Default.GetString(b);
  152. string[] tmp = s.Split((char)0);
  153. ArrayList result = new ArrayList();
  154. foreach (string r in tmp)
  155. {
  156. if (r != string.Empty)
  157. result.Add(r);
  158. }
  159. keys = new string[result.Count];
  160. for (int i = 0; i < result.Count; i++)
  161. {
  162. string[] item = result[i].ToString().Split(new char[] { '=' });
  163. if (item.Length == 2)
  164. {
  165. keys[i] = item[0].Trim();
  166. }
  167. else if (item.Length == 1)
  168. {
  169. keys[i] = item[0].Trim();
  170. }
  171. else if (item.Length == 0)
  172. {
  173. keys[i] = "";
  174. }
  175. }
  176. return 0;
  177. }
  178. /// <summary>
  179. /// 获取指定节下的Key列表
  180. /// </summary>
  181. /// <param name="section">指定的节名称</param>
  182. /// <param name="path">配置文件名称</param>
  183. /// <returns>Key列表</returns>
  184. public static List<string> GetAllKeys(string section, string path)
  185. {
  186. List<string> keyList = new List<string>();
  187. byte[] b = new byte[65535];
  188. GetPrivateProfileSection(section, b, b.Length, path);
  189. string s = System.Text.Encoding.Default.GetString(b);
  190. string[] tmp = s.Split((char)0);
  191. List<string> result = new List<string>();
  192. foreach (string r in tmp)
  193. {
  194. if (r != string.Empty)
  195. result.Add(r);
  196. }
  197. for (int i = 0; i < result.Count; i++)
  198. {
  199. string[] item = result[i].Split(new char[] { '=' });
  200. if (item.Length == 2 || item.Length == 1)
  201. {
  202. keyList.Add(item[0].Trim());
  203. }
  204. else if (item.Length == 0)
  205. {
  206. keyList.Add(string.Empty);
  207. }
  208. }
  209. return keyList;
  210. }
  211. /// <summary>
  212. /// 获取值
  213. /// </summary>
  214. /// <param name="section"></param>
  215. /// <param name="path"></param>
  216. /// <returns></returns>
  217. public static List<string> GetAllValues(string section, string path)
  218. {
  219. List<string> keyList = new List<string>();
  220. byte[] b = new byte[65535];
  221. GetPrivateProfileSection(section, b, b.Length, path);
  222. string s = System.Text.Encoding.Default.GetString(b);
  223. string[] tmp = s.Split((char)0);
  224. List<string> result = new List<string>();
  225. foreach (string r in tmp)
  226. {
  227. if (r != string.Empty)
  228. result.Add(r);
  229. }
  230. for (int i = 0; i < result.Count; i++)
  231. {
  232. string[] item = result[i].Split(new char[] { '=' });
  233. if (item.Length == 2 || item.Length == 1)
  234. {
  235. keyList.Add(item[1].Trim());
  236. }
  237. else if (item.Length == 0)
  238. {
  239. keyList.Add(string.Empty);
  240. }
  241. }
  242. return keyList;
  243. }
  244. #endregion
  245. #region 通过值查找键(一个节中的键唯一,可能存在多个键值相同,因此反查的结果可能为多个)
  246. /// <summary>
  247. /// 第一个键
  248. /// </summary>
  249. /// <param name="section"></param>
  250. /// <param name="path"></param>
  251. /// <param name="value"></param>
  252. /// <returns></returns>
  253. public static string GetFirstKeyByValue(string section, string path, string value)
  254. {
  255. foreach (string key in GetAllKeys(section, path))
  256. {
  257. if (ReadString(section, key, "", path) == value)
  258. {
  259. return key;
  260. }
  261. }
  262. return string.Empty;
  263. }
  264. /// <summary>
  265. /// 所有键
  266. /// </summary>
  267. /// <param name="section"></param>
  268. /// <param name="path"></param>
  269. /// <param name="value"></param>
  270. /// <returns></returns>
  271. public static List<string> GetKeysByValue(string section, string path, string value)
  272. {
  273. List<string > keys = new List<string>();
  274. foreach (string key in GetAllKeys(section, path))
  275. {
  276. if (ReadString(section, key, "", path) == value)
  277. {
  278. keys.Add(key);
  279. }
  280. }
  281. return keys;
  282. }
  283. #endregion
  284. #region 具体类型的读写
  285. #region string
  286. /// <summary>
  287. ///
  288. /// </summary>
  289. /// <param name="sectionName"></param>
  290. /// <param name="keyName"></param>
  291. /// <param name="defaultValue" />
  292. /// <param name="path"></param>
  293. /// <returns></returns>
  294. public static string ReadString(string sectionName, string keyName, string defaultValue, string path)
  295. {
  296. const int MAXSIZE = 255;
  297. StringBuilder temp = new StringBuilder(MAXSIZE);
  298. GetPrivateProfileString(sectionName, keyName, defaultValue, temp, 255, path);
  299. return temp.ToString();
  300. }
  301. /// <summary>
  302. ///
  303. /// </summary>
  304. /// <param name="sectionName"></param>
  305. /// <param name="keyName"></param>
  306. /// <param name="value"></param>
  307. /// <param name="path"></param>
  308. public static void WriteString(string sectionName, string keyName, string value, string path)
  309. {
  310. WritePrivateProfileString(sectionName, keyName, value, path);
  311. }
  312. #endregion
  313. #region Int
  314. /// <summary>
  315. ///
  316. /// </summary>
  317. /// <param name="sectionName"></param>
  318. /// <param name="keyName"></param>
  319. /// <param name="defaultValue"></param>
  320. /// <param name="path"></param>
  321. /// <returns></returns>
  322. public static int ReadInteger(string sectionName, string keyName, int defaultValue, string path)
  323. {
  324. return GetPrivateProfileInt(sectionName, keyName, defaultValue, path);
  325. }
  326. /// <summary>
  327. ///
  328. /// </summary>
  329. /// <param name="sectionName"></param>
  330. /// <param name="keyName"></param>
  331. /// <param name="value"></param>
  332. /// <param name="path"></param>
  333. public static void WriteInteger(string sectionName, string keyName, int value, string path)
  334. {
  335. WritePrivateProfileString(sectionName, keyName, value.ToString(), path);
  336. }
  337. #endregion
  338. #region bool
  339. /// <summary>
  340. /// 读取布尔值
  341. /// </summary>
  342. /// <param name="sectionName"></param>
  343. /// <param name="keyName"></param>
  344. /// <param name="defaultValue"></param>
  345. /// <param name="path"></param>
  346. /// <returns></returns>
  347. public static bool ReadBoolean(string sectionName, string keyName, bool defaultValue, string path)
  348. {
  349. int temp = defaultValue ? 1 : 0;
  350. int result = GetPrivateProfileInt(sectionName, keyName, temp, path);
  351. return (result == 0 ? false : true);
  352. }
  353. /// <summary>
  354. /// 写入布尔值
  355. /// </summary>
  356. /// <param name="sectionName"></param>
  357. /// <param name="keyName"></param>
  358. /// <param name="value"></param>
  359. /// <param name="path"></param>
  360. public static void WriteBoolean(string sectionName, string keyName, bool value, string path)
  361. {
  362. string temp = value ? "1 " : "0 ";
  363. WritePrivateProfileString(sectionName, keyName, temp, path);
  364. }
  365. #endregion
  366. #endregion
  367. #region 删除操作
  368. /// <summary>
  369. /// 删除指定项
  370. /// </summary>
  371. /// <param name="sectionName"></param>
  372. /// <param name="keyName"></param>
  373. /// <param name="path"></param>
  374. public static void DeleteKey(string sectionName, string keyName, string path)
  375. {
  376. WritePrivateProfileString(sectionName, keyName, null, path);
  377. }
  378. /// <summary>
  379. /// 删除指定节下的所有项
  380. /// </summary>
  381. /// <param name="sectionName"></param>
  382. /// <param name="path"></param>
  383. public static void EraseSection(string sectionName, string path)
  384. {
  385. WritePrivateProfileString(sectionName, null, null, path);
  386. }
  387. #endregion
  388. #region 判断节、键是否存在
  389. /// <summary>
  390. /// 指定节知否存在
  391. /// </summary>
  392. /// <param name="section"></param>
  393. /// <param name="fileName"></param>
  394. /// <returns></returns>
  395. public static bool ExistSection(string section, string fileName)
  396. {
  397. string[] sections = null;
  398. GetAllSectionNames(out sections, fileName);
  399. if (sections != null)
  400. {
  401. foreach (var s in sections)
  402. {
  403. if (s == section)
  404. {
  405. return true;
  406. }
  407. }
  408. }
  409. return false;
  410. }
  411. /// <summary>
  412. /// 指定节下的键是否存在
  413. /// </summary>
  414. /// <param name="section"></param>
  415. /// <param name="key"></param>
  416. /// <param name="fileName"></param>
  417. /// <returns></returns>
  418. public static bool ExistKey(string section, string key, string fileName)
  419. {
  420. string[] keys = null;
  421. GetAllKeys(section, out keys, fileName);
  422. if (keys != null)
  423. {
  424. foreach (var s in keys)
  425. {
  426. if (s == key)
  427. {
  428. return true;
  429. }
  430. }
  431. }
  432. return false;
  433. }
  434. #endregion
  435. #region 同一Section下添加多个Key\Value
  436. /// <summary>
  437. ///
  438. /// </summary>
  439. /// <param name="section"></param>
  440. /// <param name="keyList"></param>
  441. /// <param name="valueList"></param>
  442. /// <param name="path"></param>
  443. /// <returns></returns>
  444. public static bool AddSectionWithKeyValues(string section, List<string> keyList, List<string> valueList, string path)
  445. {
  446. bool bRst = true;
  447. //判断Section是否已经存在,如果存在,返回false
  448. //已经存在,则更新
  449. //if (GetAllSectionNames(path).Contains(section))
  450. //{
  451. // return false;
  452. //}
  453. //判断keyList中是否有相同的Key,如果有,返回false
  454. //添加配置信息
  455. for (int i = 0; i < keyList.Count; i++)
  456. {
  457. WriteString(section, keyList[i], valueList[i], path);
  458. }
  459. return bRst;
  460. }
  461. #endregion
  462. }
  463. }