You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.9 KiB

2 years ago
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace SqlSugar
  5. {
  6. internal class FileHelper
  7. {
  8. public static void CreateFile(string filePath, string text, Encoding encoding)
  9. {
  10. try
  11. {
  12. if (IsExistFile(filePath))
  13. {
  14. DeleteFile(filePath);
  15. }
  16. if (!IsExistFile(filePath))
  17. {
  18. var directoryPath = GetDirectoryFromFilePath(filePath);
  19. CreateDirectory(directoryPath);
  20. //Create File
  21. var file = new FileInfo(filePath);
  22. using (FileStream stream = file.Create())
  23. {
  24. using (StreamWriter writer = new StreamWriter(stream, encoding))
  25. {
  26. writer.Write(text);
  27. writer.Flush();
  28. }
  29. }
  30. }
  31. }
  32. catch(Exception ex)
  33. {
  34. throw ex;
  35. }
  36. }
  37. public static bool IsExistDirectory(string directoryPath)
  38. {
  39. return Directory.Exists(directoryPath);
  40. }
  41. public static void CreateDirectory(string directoryPath)
  42. {
  43. if (!IsExistDirectory(directoryPath))
  44. {
  45. Directory.CreateDirectory(directoryPath);
  46. }
  47. }
  48. public static void DeleteFile(string filePath)
  49. {
  50. if (IsExistFile(filePath))
  51. {
  52. File.Delete(filePath);
  53. }
  54. }
  55. public static string GetDirectoryFromFilePath(string filePath)
  56. {
  57. var file = new FileInfo(filePath);
  58. var directory = file.Directory;
  59. return directory.FullName;
  60. }
  61. public static bool IsExistFile(string filePath)
  62. {
  63. return File.Exists(filePath);
  64. }
  65. }
  66. }