namespace XGame.Framework.FileSystem { /// /// 文件操作 /// public static partial class File { private static readonly IFileOperator _fileOperator = CreateFileOperator(); private static readonly IDirectoryOperator _directoryOperator = CreateDirectoryOperator(); #region File Operator /// /// 获取文件名 /// /// /// /// public static string GetFileName(string fullName, bool withoutExt = true) { return _fileOperator.GetFileName(fullName, withoutExt); } /// /// 拓展名 /// /// 文件全路径(含拓展名) /// 拓展名(不包含“.”) public static string Extension(string fullName) { return _fileOperator.Extension(fullName); } /// /// 文件是否存在(包外全路径) /// /// 文件全路径(含拓展名) /// True: 存在; False: 不存在; public static bool ExistFile(string fullName) { return _fileOperator.Exist(fullName); } /// /// 删除文件(包外全路径) /// /// 文件全路径(含拓展名) /// True: 删除成功; False: 删除失败; public static bool DeleteFile(string fullName) { return _fileOperator.Delete(fullName); } /// /// 获取文件所在文件夹路径(包外全路径) /// /// 文件全路径(含拓展名) /// 文件夹全路径 public static string GetDirectory(string fullName) { return _fileOperator.GetDirectory(fullName); } /// /// 复制文件(包外全路径) /// /// 源文件 /// 目标文件路径 /// True: 复制成功; False: 复制失败; public static bool CopyFile(string source, string destination) { return _fileOperator.Copy(source, destination, true); } /// /// 异步复制文件 /// /// 源文件 /// 包外文件全路径 /// 覆盖 /// 复制文件异步对象 public static ICopyFileAsync CopyFileAsync(string source, string destination, bool overwrite = true) { return _fileOperator.CopyFileAsync(source, destination, overwrite); } /// /// 移动文件(包外全路径) /// /// 源文件 /// 目标文件路径 public static void MoveFile(string source, string destination) { _fileOperator.Move(source, destination); } /// /// 写入字节数组(包外全路径) /// /// 文件全路径(包外) /// 内容 public static void WriteAllBytes(string filePath, byte[] bytes) { _fileOperator.WriteAllBytes(filePath, bytes); } /// /// 写入文本(包外全路径) /// /// 文件全路径(包外) /// public static void WriteAllText(string filePath, string contents) { _fileOperator.WriteAllText(filePath, contents); } /// /// 读取文件 /// /// 文件全路径 /// 字节流 public static byte[] ReadAllBytes(string path) { return _fileOperator.ReadAllBytes(path); } /// /// 读取文本(UTF-8) /// /// 文件全路径 /// 文本 public static string ReadAllText(string path) { return _fileOperator.ReadAllText(path); } /// /// 异步读取文件 /// /// 文件全路径(包内包外都可以) /// 文件内容 public static IReadFileAsync ReadFileAsync(string filePath) { return _fileOperator.ReadFileAsync(filePath); } /// /// 重命名文件 /// /// /// /// public static bool RenameFile(string from, string to) { return _fileOperator.Rename(from, to); } #endregion #region Directory Operator /// /// 新建文件夹(逐级新建) /// /// 文件夹全路径 /// True: 新建成功; False: 新建失败 public static bool Mkdir(string fullDirectory) { return _directoryOperator.Mkdir(fullDirectory); } /// /// 删除文件夹(包外全路径) /// /// 文件夹全路径 /// 是否删除自身文件夹 /// True: 删除成功; False: 删除失败; public static bool DeleteDirectory(string fullDirectory, bool includeSelf = true) { return _directoryOperator.Delete(fullDirectory, includeSelf); } /// /// 文件夹是否存在(包外全路径) /// /// 文件夹全路径 /// True: 存在; False: 不存在; public static bool ExistDirectory(string fullDirectory) { return _directoryOperator.Exist(fullDirectory); } /// /// 是否是文件夹(包外全路径) /// /// 文件夹全路径 /// True: 是; False: 不是; public static bool IsDirectory(string fullDirectory) { return _directoryOperator.Exist(fullDirectory); } /// /// 复制(含子文件夹和子文件) /// /// 源文件夹 /// 目标文件夹(清空) /// True: 复制成功; False: 复制失败 public static bool CopyDirectory(string source, string destination) { return _directoryOperator.Copy(source, destination, true); } /// /// 获取文件夹名 /// /// 文件夹全路径 /// 文件夹名(最后一个"/") public static string GetDirectoryName(string fullDirectory) { return _directoryOperator.GetDirectoryName(fullDirectory); } /// /// 获取文件夹下的所有文件和文件夹路径 /// /// 文件夹全路径 /// public static string[] GetFileSystemEntries(string fullDirectory) { return _directoryOperator.GetFileSystemEntries(fullDirectory); } /// /// 异步删除文件夹 /// /// 文件夹全路径 /// 是否删除自身文件夹 /// True: 删除成功; False: 删除失败; public static IDeleteDirectoryAsync DeleteAsync(string fullDirectory, bool includeSelf = true) { return _directoryOperator.DeleteAsync(fullDirectory, includeSelf); } /// /// 重命名文件夹 /// /// /// /// public static bool RenameDirectory(string from, string to) { Assert.IsTrue(ExistDirectory(from), $"File.RenameDirectory: {from} 文件夹不存在!!"); Assert.IsTrue(!ExistDirectory(to), $"File.RenameDirectory: {to} 文件夹已存在!!"); return _directoryOperator.Rename(from, to); } /// /// 获取文件夹下所有文件 /// /// 根文件夹全路径 /// 递归 /// 文件全路径 public static string[] GetFiles(string fullDirectory, bool recursion = true) { return _directoryOperator.GetFiles(fullDirectory, null, recursion); } /// /// 获取文件夹下指定拓展名文件 /// /// 根文件夹全路径 /// 拓展名(".xxx") /// 递归 /// 文件全路径 public static string[] GetFiles(string fullDirectory, string extension, bool recursion = true) { return _directoryOperator.GetFiles(fullDirectory, extension, recursion); } #endregion #region Factory static IFileOperator CreateFileOperator() { return new CSharpFileOperator(); } static IDirectoryOperator CreateDirectoryOperator() { return new CSharpDirectoryOperator(); } #endregion #region Path /// /// 路径拼接 /// /// /// public static string Combine(params string[] paths) { return System.IO.Path.Combine(paths).Replace('\\', '/'); } #endregion } }