INI.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace ErpServer
  5. {
  6. public class INI
  7. {
  8. /// <summary>
  9. /// 获取所有节点名称(Section)
  10. /// </summary>
  11. /// <param name="lpszReturnBuffer">存放节点名称的内存地址,每个节点之间用\0分隔</param>
  12. /// <param name="nSize">内存大小(characters)</param>
  13. /// <param name="lpFileName">Ini文件</param>
  14. /// <returns>内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够</returns>
  15. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  16. private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);
  17. /// <summary>
  18. /// 写入INI文件
  19. /// </summary>
  20. /// <param name="section">节点名称[如[TypeName]]</param>
  21. /// <param name="key">键</param>
  22. /// <param name="val">值</param>
  23. /// <param name="filepath">文件路径</param>
  24. /// <returns></returns>
  25. [DllImport("kernel32")]
  26. private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
  27. /// <summary>
  28. /// 读取INI文件
  29. /// </summary>
  30. /// <param name="section">节点名称</param>
  31. /// <param name="key">键</param>
  32. /// <param name="def">值</param>
  33. /// <param name="retval">stringbulider对象</param>
  34. /// <param name="size">字节大小</param>
  35. /// <param name="filePath">文件路径</param>
  36. /// <returns></returns>
  37. [DllImport("kernel32")]
  38. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
  39. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  40. private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
  41. /// <summary>
  42. /// 自定义读取INI文件中的内容方法
  43. /// </summary>
  44. /// <param name="Section">键</param>
  45. /// <param name="key">值</param>
  46. /// <returns></returns>
  47. public static string GetIniValue(string Section, string key, string filePath)
  48. {
  49. StringBuilder temp = new StringBuilder(1024);
  50. GetPrivateProfileString(Section, key, "", temp, 1024, filePath);
  51. return temp.ToString();
  52. }
  53. public static void SetIniValue(string Section, string key, string value, string filePath)
  54. {
  55. WritePrivateProfileString(Section, key, value, filePath);
  56. }
  57. /// <summary>
  58. /// 根据section取所有key
  59. /// </summary>
  60. /// <param name="section"></param>
  61. /// <param name="filePath"></param>
  62. /// <returns></returns>
  63. public static string[] ReadIniAllKeys(string section, string filePath)
  64. {
  65. UInt32 MAX_BUFFER = 32767;
  66. string[] items = new string[0];
  67. IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
  68. UInt32 bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, filePath);
  69. if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
  70. {
  71. string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
  72. items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
  73. }
  74. Marshal.FreeCoTaskMem(pReturnedString);
  75. return items;
  76. }
  77. /// <summary>
  78. /// 读取INI文件中指定INI文件中的所有节点名称(Section)
  79. /// </summary>
  80. /// <param name="iniFile">Ini文件</param>
  81. /// <returns>所有节点,没有内容返回string[0]</returns>
  82. public static string[] INIGetAllSectionNames(string iniFile)
  83. {
  84. uint MAX_BUFFER = 32767; //默认为32767
  85. string[] sections = new string[0]; //返回值
  86. //申请内存
  87. IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
  88. uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);
  89. if (bytesReturned != 0)
  90. {
  91. //读取指定内存的内容
  92. string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
  93. //每个节点之间用\0分隔,末尾有一个\0
  94. sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
  95. }
  96. //释放内存
  97. Marshal.FreeCoTaskMem(pReturnedString);
  98. return sections;
  99. }
  100. /// <summary>
  101. /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
  102. /// </summary>
  103. /// <param name="iniFile">Ini文件</param>
  104. /// <param name="section">节点名称</param>
  105. /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns>
  106. public static string[] INIGetAllItems(string iniFile, string section)
  107. {
  108. //返回值形式为 key=value,例如 Color=Red
  109. uint MAX_BUFFER = 32767; //默认为32767
  110. string[] items = new string[0]; //返回值
  111. //分配内存
  112. IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
  113. uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);
  114. if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
  115. {
  116. string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
  117. items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
  118. }
  119. Marshal.FreeCoTaskMem(pReturnedString); //释放内存
  120. return items;
  121. }
  122. }
  123. }