FilesHelper.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.IO;
  3. namespace Common
  4. {
  5. public class FilesHelper
  6. {
  7. public static void WriteFile(string path, string data)
  8. {
  9. using (var fs = new FileStream(path, FileMode.Append))
  10. {
  11. using (var sw = new StreamWriter(fs))
  12. {
  13. sw.WriteLine(data);
  14. sw.Flush();
  15. }
  16. }
  17. }
  18. public static string ReaderFile(string path)
  19. {
  20. using (var sw = new StreamReader(path))
  21. {
  22. return sw.ReadToEnd();
  23. }
  24. }
  25. public static void LogBD(string content, string pathName = "", string directoryName = "Log")
  26. {
  27. if (pathName.IsEmpty())
  28. pathName = content;
  29. var path = AppDomain.CurrentDomain.BaseDirectory + "/" + directoryName;
  30. CreateDirectory(path);
  31. path += $"/{DateTime.Now.ToString("yyyyMMdd")}";
  32. CreateDirectory(path);
  33. WriteFile(path + $"/{pathName}.txt", content + " || " + DateTime.Now.ToString());
  34. }
  35. /// <summary>
  36. /// 创建文件夹
  37. /// </summary>
  38. /// <param name="paht"></param>
  39. private static void CreateDirectory(string path)
  40. {
  41. if (!Directory.Exists(path))
  42. Directory.CreateDirectory(path);
  43. }
  44. }
  45. }