FileManager.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.IO;
  2. public static class FileManager
  3. {
  4. /// <summary>
  5. /// 读取文件
  6. /// </summary>
  7. /// <param name="filePath">文件路径</param>
  8. /// <returns></returns>
  9. public static string ReadFile(string filePath)
  10. {
  11. if (!File.Exists(filePath))
  12. {
  13. WriteFile("[]", filePath);
  14. return "[]";
  15. }
  16. return File.ReadAllText(filePath);
  17. }
  18. /// <summary>
  19. /// 写入文件
  20. /// </summary>
  21. /// <param name="str">写入的内容</param>
  22. /// <param name="filePath">写入路径</param>
  23. public static void WriteFile(string str, string filePath)
  24. {
  25. if (Directory.Exists(filePath) == false)
  26. {
  27. DirectoryInfo pathInfo = new DirectoryInfo(filePath);
  28. pathInfo.Parent.Create();
  29. }
  30. File.WriteAllText(filePath, str);
  31. }
  32. /// <summary>
  33. /// 检查文件路径
  34. /// </summary>
  35. /// <param name="path"></param>
  36. /// <returns></returns>
  37. public static string CalFilePath(string path)
  38. {
  39. if (path.Contains("\\"))
  40. {
  41. path = path.Replace("\\", "/");
  42. }
  43. return path;
  44. }
  45. }