FileHelper.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.IO;
  2. using System.Text;
  3. namespace Vinno.FIS.Sonopost.Helpers
  4. {
  5. public class FileHelper
  6. {
  7. /// <summary>
  8. /// Save file
  9. /// </summary>
  10. public static void Save(string path, string content)
  11. {
  12. var data = new UTF8Encoding().GetBytes(content);
  13. using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
  14. {
  15. stream.Write(data, 0, data.Length);
  16. }
  17. }
  18. public static string GetText(string path)
  19. {
  20. using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))//Felix:若改为ReadWrite,SmartLogs会提示文件被占用。
  21. {
  22. byte[] buffer = new byte[stream.Length];
  23. stream.Read(buffer, 0, buffer.Length);
  24. string text = Encoding.UTF8.GetString(buffer);
  25. return text;
  26. }
  27. }
  28. public static void CopyStream(string path, Stream destStream)
  29. {
  30. using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
  31. {
  32. fs.CopyTo(destStream);
  33. }
  34. }
  35. public static bool Exist(string path)
  36. {
  37. return File.Exists(path);
  38. }
  39. }
  40. }