FileService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Wms.Screen.Service.IService;
  7. namespace Wms.Screen.Service.Service
  8. {
  9. /// <summary>
  10. /// INI文件操作方法
  11. /// </summary>
  12. /// creater maya
  13. /// createon 2020-09-08
  14. public class FileService : IFileService
  15. {
  16. public string IniPath { get; set; }
  17. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  18. public struct STRINGBUFFER
  19. {
  20. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  21. public string szText;
  22. }
  23. //读写INI文件的API函数
  24. [DllImport("kernel32", CharSet = CharSet.Auto)]
  25. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  26. [DllImport("kernel32", CharSet = CharSet.Auto)]
  27. private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath);
  28. [DllImport("kernel32")]
  29. private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
  30. /// 读取INI文件中指定的Key的值
  31. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  32. private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, [In, Out] char[] lpReturnedString, uint nSize, string lpFileName);
  33. //另一种声明方式,使用 StringBuilder 作为缓冲区类型的缺点是不能接受\0字符,会将\0及其后的字符截断,
  34. //所以对于lpAppName或lpKeyName为null的情况就不适用
  35. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  36. private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
  37. //再一种声明,使用string作为缓冲区的类型同char[]
  38. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  39. private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, uint nSize, string lpFileName);
  40. //类的构造函数,传递INI档案名
  41. public FileService()
  42. {
  43. IniPath = $@"{Directory.GetCurrentDirectory()}\ZhongTian.ini";
  44. if (!File.Exists(IniPath))
  45. {
  46. CreateIniFile();
  47. }
  48. }
  49. //写INI文件
  50. public void IniWriteValue(string Section, string Key, string Value)
  51. {
  52. WritePrivateProfileString(Section, Key, Value, this.IniPath);
  53. }
  54. //读取INI文件指定
  55. public string IniReadValue(string Section, string Key)
  56. {
  57. //int i;
  58. //const int SIZE = 1024 * 10;
  59. //STRINGBUFFER RetVal;
  60. //i = GetPrivateProfileString(Section, Key, null, out RetVal, SIZE, this.IniPath);
  61. //string temp = RetVal.szText;
  62. //return temp.Trim();
  63. string value = "";
  64. const int SIZE = 1024 * 10;
  65. StringBuilder sb = new StringBuilder(SIZE);
  66. uint bytesReturned = GetPrivateProfileString(Section, Key, "", sb, SIZE, IniPath);
  67. if (bytesReturned != 0)
  68. {
  69. value = sb.ToString();
  70. }
  71. return value;
  72. }
  73. /// <summary>
  74. /// 创建INI文件
  75. /// </summary>
  76. public void CreateIniFile()
  77. {
  78. StreamWriter w = File.CreateText(IniPath);
  79. w.Write("");
  80. w.Flush();
  81. w.Close();
  82. }
  83. #region 读取Section下的所有Key
  84. /// <summary>
  85. /// 读取Section下的所有Key,section为ini文件中的项目,通过该函数将对应项目中的变量都提取出来保存到KeyList中
  86. /// </summary>
  87. /// <param name="Section"></param>
  88. public List<string> ReadKeyList(string Section)
  89. {
  90. List<string> StrList = new List<string>();
  91. byte[] TmpBuf = new byte[40960];
  92. int Len = GetPrivateProfileString(Section, null, null, TmpBuf, TmpBuf.Length, this.IniPath);
  93. StrList = SplitBufToStrList(TmpBuf, Len);
  94. return StrList;
  95. }
  96. private List<string> SplitBufToStrList(byte[] Buffer, int bufLen)
  97. {
  98. List<string> StrList = new List<string>();
  99. if (bufLen != 0)
  100. {
  101. int start = 0;
  102. for (int i = 0; i < bufLen; i++)
  103. {
  104. if ((Buffer[i] == 0) && ((i - start) > 0))
  105. {
  106. string TmpStr = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
  107. StrList.Add(TmpStr);
  108. start = i + 1;
  109. }
  110. }
  111. }
  112. return StrList;
  113. }
  114. #endregion
  115. }
  116. }