123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System.IO;
- public static class FileManager
- {
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static string ReadFile(string filePath)
- {
- if (!File.Exists(filePath))
- {
- WriteFile("[]", filePath);
- return "[]";
- }
-
- return File.ReadAllText(filePath);
- }
- /// <summary>
- /// 写入文件
- /// </summary>
- /// <param name="str">写入的内容</param>
- /// <param name="filePath">写入路径</param>
- public static void WriteFile(string str, string filePath)
- {
- if (Directory.Exists(filePath) == false)
- {
- DirectoryInfo pathInfo = new DirectoryInfo(filePath);
- pathInfo.Parent.Create();
- }
- File.WriteAllText(filePath, str);
- }
- /// <summary>
- /// 检查文件路径
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string CalFilePath(string path)
- {
- if (path.Contains("\\"))
- {
- path = path.Replace("\\", "/");
- }
- return path;
- }
- }
|