FileFunction.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. namespace BZFAStandardLib
  8. {
  9. /// <summary>
  10. /// txt文件操作专用类
  11. /// </summary>
  12. public static class FileOperate
  13. {
  14. private static Mutex mutexNew = new Mutex();
  15. private static Mutex mutexWrite = new Mutex();
  16. /// <summary>
  17. /// 新建文件,
  18. /// </summary>
  19. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  20. public static void NewTxtFile(string txtFile)
  21. {
  22. string path = txtFile.Substring(0, txtFile.LastIndexOf("\\"));
  23. if (!Directory.Exists(path)) Directory.CreateDirectory(path); //判断路径是否存在,不存在则创建
  24. if (!File.Exists(txtFile)) //判断文件是否存在,不存在则创建
  25. {
  26. using (FileStream fileStream = new FileStream(txtFile, FileMode.Create, FileAccess.Write))
  27. {
  28. fileStream.Close();
  29. }
  30. }
  31. }
  32. /// <summary>
  33. /// 新建并写入txt文件;
  34. /// </summary>
  35. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  36. /// <param name="txt">写入内容</param>
  37. public static void NewTxtFile(string txtFile, string txt)
  38. {
  39. string str1 = "\r\n====================================================================\r\n";
  40. string str2 = "\r\n====================================================================";
  41. string path = txtFile.Substring(0, txtFile.LastIndexOf("\\"));
  42. bool fileExists = File.Exists(txtFile);
  43. // 如果文件不存在,则创建文件并写入内容
  44. if (!fileExists)
  45. {
  46. using (FileStream fileStream = new FileStream(txtFile, FileMode.Create, FileAccess.Write))
  47. using (StreamWriter streamWriter = new StreamWriter(fileStream))
  48. {
  49. streamWriter.Write(str1 + txt + str2); // 直接写入内容
  50. }
  51. }
  52. else
  53. {
  54. // 如果文件存在,则打开文件并在末尾追加内容
  55. using (FileStream fileStream = new FileStream(txtFile, FileMode.Append, FileAccess.Write))
  56. using (StreamWriter streamWriter = new StreamWriter(fileStream))
  57. {
  58. streamWriter.WriteLine(str1 + txt + str2); // 使用 WriteLine 确保有换行符
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// 带锁
  64. /// </summary>
  65. /// <param name="txtFile"></param>
  66. public static void NewFile(string file)
  67. {
  68. try
  69. {
  70. mutexNew.WaitOne();
  71. string path = file.Substring(0, file.LastIndexOf("\\"));
  72. if (!Directory.Exists(path)) Directory.CreateDirectory(path); //判断路径是否存在,不存在则创建
  73. if (!File.Exists(file)) //判断文件是否存在,不存在则创建
  74. {
  75. using (FileStream fileStream = new FileStream(file, FileMode.Create, FileAccess.Write))
  76. {
  77. fileStream.Close();
  78. }
  79. }
  80. mutexNew.ReleaseMutex();
  81. }
  82. catch (Exception ex)
  83. {
  84. mutexNew.ReleaseMutex();
  85. }
  86. }
  87. /// <summary>
  88. /// 写入txt文件;
  89. /// </summary>
  90. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  91. /// <param name="txt">写入内容</param>
  92. public static bool WriteTxtFile(string txtFile, string txt)
  93. {
  94. if (!File.Exists(txtFile)) NewTxtFile(txtFile); //判断文件是否存在,不存在则新建;
  95. try
  96. {
  97. using (FileStream fileStream = new FileStream(txtFile, FileMode.Open, FileAccess.Write)) //写入文件
  98. {
  99. StreamWriter streamWriter = new StreamWriter(fileStream);
  100. //sw.BaseStream的Position或Seek()可移动文件流指针到的任意位置;
  101. streamWriter.BaseStream.Seek(0, SeekOrigin.End); //在txt末尾写入;
  102. streamWriter.Write(txt); //写入txt文件内容;
  103. streamWriter.Flush();
  104. streamWriter.Close();
  105. fileStream.Close();
  106. }
  107. return true;
  108. }
  109. catch { return false; }
  110. }
  111. /// <summary>
  112. /// 带锁
  113. /// </summary>
  114. /// <param name="txtFile"></param>
  115. /// <param name="txt"></param>
  116. public static void WriteFile(string file, string txt)
  117. {
  118. try
  119. {
  120. mutexWrite.WaitOne();
  121. if (!File.Exists(file)) NewFile(file); //判断文件是否存在,不存在则新建;
  122. using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Write)) //写入文件
  123. {
  124. StreamWriter streamWriter = new StreamWriter(fileStream);
  125. //sw.BaseStream的Position或Seek()可移动文件流指针到的任意位置;
  126. streamWriter.BaseStream.Seek(0, SeekOrigin.End); //在txt末尾写入;
  127. streamWriter.Write(txt); //写入txt文件内容;
  128. streamWriter.Flush();
  129. streamWriter.Close();
  130. fileStream.Close();
  131. }
  132. mutexWrite.ReleaseMutex();
  133. }
  134. catch
  135. {
  136. mutexWrite.ReleaseMutex();
  137. }
  138. }
  139. /// <summary>
  140. /// 读取txt文件内容;
  141. /// </summary>
  142. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  143. /// <returns>返回txt文件内容</returns>
  144. public static string ReadTxtFile(string txtFile)
  145. {
  146. if (!File.Exists(txtFile)) return ""; //判断文件是否存在;
  147. string Return = "";
  148. using (FileStream fileStream = new FileStream(txtFile, FileMode.Open, FileAccess.Read))
  149. {
  150. StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8);
  151. Return = streamReader.ReadToEnd().ToString();
  152. streamReader.Close();
  153. fileStream.Close();
  154. }
  155. return Return;
  156. }
  157. /// <summary>
  158. /// 读取最后一行内容;
  159. /// </summary>
  160. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  161. /// <returns></returns>
  162. public static string ReadLastLine(string txtFile)
  163. {
  164. if (!File.Exists(txtFile)) return ""; //判断文件是否存在;
  165. string[] allLines = File.ReadAllLines(txtFile);
  166. if (allLines.Length > 0)
  167. return allLines[allLines.Length - 1];
  168. else return "";
  169. }
  170. /// <summary>
  171. /// 覆盖最后一行内容;
  172. /// </summary>
  173. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  174. /// <param name="txt"></param>
  175. public static void CoverLastLine(string txtFile, string txt)
  176. {
  177. if (File.Exists(txtFile)) //判断文件是否存在;
  178. {
  179. string[] allLines = File.ReadAllLines(txtFile);
  180. if (allLines.Length > 0)
  181. {
  182. allLines[allLines.Length - 1] = txt;
  183. File.WriteAllLines(txtFile, allLines);
  184. }
  185. }
  186. }
  187. /// <summary>
  188. /// 移除最后一行内容;
  189. /// </summary>
  190. /// <param name="txtFile">文件路径+文件名(包含扩展名)</param>
  191. public static void RemoveLastLine(string txtFile)
  192. {
  193. if (File.Exists(txtFile)) //判断文件是否存在
  194. {
  195. string[] allLines = File.ReadAllLines(txtFile);
  196. if (allLines.Length > 0)
  197. {
  198. Array.Resize(ref allLines, allLines.Length - 1);
  199. File.WriteAllLines(txtFile, allLines);
  200. }
  201. }
  202. }
  203. }
  204. }