FileHelper.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 vCloud.Windows.ForceUpgrade
  8. {
  9. public static class FileHelper
  10. {
  11. /// <summary>
  12. /// 追加写入文件
  13. /// </summary>
  14. /// <param name="fileName">文件名</param>
  15. /// <param name="contexts">文件内容</param>
  16. /// <param name="encodingStr">文件编码</param>
  17. public static void AppendWriteFile(string fileName, string contexts, string encodingStr = "UTF-8")
  18. {
  19. try
  20. {
  21. if (!string.IsNullOrEmpty(fileName))
  22. {
  23. using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
  24. {
  25. Encoding textEncoding = Encoding.GetEncoding(encodingStr.ToUpper());
  26. using (StreamWriter streamWriter = new StreamWriter(fs, textEncoding))
  27. {
  28. streamWriter.Write(contexts);
  29. }
  30. }
  31. }
  32. }
  33. catch
  34. {
  35. }
  36. }
  37. }
  38. }