FileHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MECF.Framework.Common.Utilities
  8. {
  9. public class FileHelper
  10. {
  11. //定义一个用于保存静态变量的实例
  12. private static FileHelper instance = null;
  13. //定义一个保证线程同步的标识
  14. private static readonly object locker = new object();
  15. //构造函数为私有,使外界不能创建该类的实例
  16. private FileHelper() { }
  17. public static FileHelper Instance
  18. {
  19. get
  20. {
  21. if (instance == null)
  22. {
  23. lock (locker)
  24. {
  25. if (instance == null) instance = new FileHelper();
  26. }
  27. }
  28. return instance;
  29. }
  30. }
  31. public void SaveNewLineDataToTxt(string dir, string filePath, string text, bool isAppend = false)
  32. {
  33. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  34. filePath = filePath + ".txt";
  35. using (StreamWriter sw = new StreamWriter(filePath, isAppend, Encoding.UTF8))
  36. {
  37. sw.WriteLine(text);
  38. sw.Flush();
  39. sw.Close();
  40. }
  41. }
  42. public void SaveNewLineDataToTxt(string fullFilePath, string text, bool isAppend = false)
  43. {
  44. string dir = fullFilePath.Substring(0, fullFilePath.LastIndexOf('\\'));
  45. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  46. using (StreamWriter sw = new StreamWriter(fullFilePath, isAppend, Encoding.UTF8))
  47. {
  48. sw.WriteLine(text);
  49. sw.Flush();
  50. sw.Close();
  51. }
  52. }
  53. public void SaveText(string fullFileName, string WriteTxt)
  54. {
  55. try
  56. {
  57. string dir= fullFileName.Substring(0, fullFileName.LastIndexOf('\\'));
  58. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  59. FileStream fs = new FileStream(fullFileName,
  60. FileMode.OpenOrCreate, FileAccess.Write);
  61. StreamWriter streamWriter = new StreamWriter(fs, Encoding.UTF8);
  62. streamWriter.Flush();
  63. streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  64. streamWriter.WriteLine(WriteTxt);
  65. streamWriter.Flush();
  66. streamWriter.Close();
  67. }
  68. catch { }
  69. }
  70. public void SaveText(string dir, string fileName, string WriteTxt)
  71. {
  72. try
  73. {
  74. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  75. FileStream fs = new FileStream(dir + "\\" + fileName + ".txt",
  76. FileMode.OpenOrCreate, FileAccess.Write);
  77. StreamWriter streamWriter = new StreamWriter(fs, Encoding.UTF8);
  78. streamWriter.Flush();
  79. streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  80. streamWriter.WriteLine(WriteTxt);
  81. streamWriter.Flush();
  82. streamWriter.Close();
  83. }
  84. catch { }
  85. }
  86. public string ReadAllText(string filePath)
  87. {
  88. if (!File.Exists(filePath)) return filePath + ",文件不存在";
  89. return File.ReadAllText(filePath);
  90. }
  91. public string ReadFileStream(string filePath)
  92. {
  93. string result = string.Empty;
  94. try
  95. {
  96. if (!File.Exists(filePath)) return filePath + ",文件不存在";
  97. FileStream fileStream = new FileStream(filePath, FileMode.Open,
  98. FileAccess.ReadWrite, FileShare.ReadWrite);
  99. StreamReader sr = new StreamReader(fileStream, Encoding.Default);
  100. result = sr.ReadToEnd();
  101. sr.Close();
  102. fileStream.Close();
  103. }
  104. catch { return result; }
  105. return result;
  106. }
  107. }
  108. }