IniFile.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Text;
  2. using System.Runtime.InteropServices;
  3. using System.Management;
  4. using System;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace Ini
  8. {
  9. public class IniFile
  10. {
  11. public string path; //INI文件名 111
  12. public string HISIP;
  13. public string HISURL;
  14. public string HISauthorization;
  15. [DllImport("kernel32")]
  16. private static extern long WritePrivateProfileString(string section, string key,
  17. string val, string filePath);
  18. [DllImport("kernel32")]
  19. private static extern int GetPrivateProfileString(string section, string key, string def,
  20. StringBuilder retVal, int size, string filePath);
  21. //声明读写INI文件的API函数
  22. public IniFile()
  23. {
  24. try
  25. {
  26. path = System.IO.Directory.GetCurrentDirectory() + @"\INSUConfigure.ini";
  27. if (System.IO.File.Exists(path) == false)
  28. {
  29. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//INSUConfigure.ini";
  30. }
  31. }
  32. catch (System.Exception ex)
  33. {
  34. path = "C://ProgramData//XYS//XYSService//plugins//ReadYBCard//INSUConfigure.ini";
  35. }
  36. }
  37. //类的构造函数,传递INI文件名
  38. //写INI文件
  39. public void IniWriteValue(string Section, string Key, string Value)
  40. {
  41. WritePrivateProfileString(Section, Key, Value, this.path);
  42. }
  43. //读取INI文件指定
  44. public string IniReadValue(string Section, string Key)
  45. {
  46. StringBuilder temp = new StringBuilder(255);
  47. int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
  48. return temp.ToString();
  49. }
  50. public string IniReadLogPath()
  51. {
  52. return IniReadValue("LOG", "path");
  53. }
  54. /// <summary>
  55. /// 获取本机的MAC地址
  56. /// </summary>
  57. /// <returns></returns>
  58. public string GetMACString()
  59. {
  60. ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
  61. ManagementObjectCollection mo = mAdapter.GetInstances();
  62. foreach (ManagementBaseObject m in mo)
  63. {
  64. if ((bool)m["IpEnabled"] == true)
  65. {
  66. return m["MacAddress"].ToString();
  67. }
  68. }
  69. mo.Dispose();
  70. return null;
  71. }
  72. }
  73. class FileClass
  74. {
  75. //创建文件夹
  76. //参数:path 文件夹路径
  77. public bool CreateFolder(string path)
  78. {
  79. try
  80. {
  81. if (Directory.Exists(path))
  82. {
  83. return true;
  84. }
  85. if (!Directory.Exists(path.Substring(0, path.LastIndexOf("\\"))))
  86. { //若路径中无“\”则表示路径错误
  87. return false;
  88. }
  89. else
  90. {
  91. //创建文件夹
  92. DirectoryInfo dirInfo = Directory.CreateDirectory(path);
  93. return true;
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. return false;
  99. }
  100. }
  101. //创建文件
  102. //参数:path 文件路径
  103. public void CreateFile(string path)
  104. {
  105. try
  106. {
  107. if (CreateFolder(path.Substring(0, path.LastIndexOf("\\"))))
  108. {
  109. if (!File.Exists(path))
  110. {
  111. FileStream fs = File.Create(path);
  112. fs.Close();
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. return;
  119. }
  120. }
  121. //删除文件
  122. //参数:path 文件夹路径
  123. public void DeleteFile(string path)
  124. {
  125. try
  126. {
  127. if (!File.Exists(path))
  128. {
  129. return;
  130. }
  131. else
  132. {
  133. File.Delete(path);
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. return;
  139. }
  140. }
  141. //写文件
  142. //参数:path 文件夹路径 、content要写的内容
  143. public void WriteFile(string path, string content)
  144. {
  145. try
  146. {
  147. if (!File.Exists(path))
  148. {
  149. CreateFile(path);
  150. }
  151. FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
  152. StreamWriter sw = new StreamWriter(fs);
  153. sw.WriteLine(content);
  154. sw.Close();
  155. }
  156. catch (Exception ex)
  157. {
  158. return;
  159. }
  160. }
  161. /// <summary>
  162. /// 将即时日志保存入日志文件
  163. /// </summary>
  164. public void WriteLogFile( string content)
  165. {
  166. string directoryPath = System.AppDomain.CurrentDomain.BaseDirectory + "YBLog";
  167. if (!Directory.Exists(directoryPath))
  168. {
  169. CreateFolder(directoryPath);
  170. }
  171. try
  172. {
  173. //写入新的文件
  174. string filePath = directoryPath + "\\" + DateTime.Now.Date.ToString("yyyyMMdd") + ".log";
  175. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
  176. StreamWriter sw = new StreamWriter(fs);
  177. sw.WriteLine(content);
  178. sw.Close();
  179. fs.Close();
  180. }
  181. catch (Exception ex)
  182. {
  183. }
  184. }
  185. }
  186. }