FileUtil.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace LYFZ.Software.MainBusiness.MultipleFileImport
  7. {
  8. public class FileUtil
  9. {
  10. public static bool CheckAndCreateDirectory(string dirPath)
  11. {
  12. try
  13. {
  14. if (!Directory.Exists(dirPath))
  15. {
  16. Directory.CreateDirectory(dirPath);
  17. }
  18. return true;
  19. }
  20. catch {
  21. return false;
  22. }
  23. }
  24. public static string ReadFile(string filePaht)
  25. {
  26. if (File.Exists(filePaht))
  27. {
  28. return File.ReadAllText(filePaht);
  29. }
  30. else
  31. {
  32. return "";
  33. }
  34. }
  35. public static void WriteFile(string filePath,string txt)
  36. {
  37. string dir = Path.GetDirectoryName(filePath);
  38. if(CheckAndCreateDirectory(dir))
  39. {
  40. File.WriteAllText(filePath, txt);
  41. }
  42. }
  43. public static void WriteFileAppend(string filePath, string txt)
  44. {
  45. string dir = Path.GetDirectoryName(filePath);
  46. if (CheckAndCreateDirectory(dir))
  47. {
  48. File.AppendAllText(filePath, txt);
  49. }
  50. }
  51. public static void WriteFileByASCII(string filePath, string txt)
  52. {
  53. string dir = Path.GetDirectoryName(filePath);
  54. if (CheckAndCreateDirectory(dir))
  55. {
  56. File.WriteAllText(filePath, txt, Encoding.ASCII);
  57. }
  58. }
  59. }
  60. }