using System; using System.ComponentModel; using System.IO; using System.Threading.Tasks; using UnityEngine; namespace ZXL.Helper { public static class FileHelper { #region File /// /// 创建文件,并写入内容 /// /// /// public static void CreateFile(string srcFilePath, byte[] bytes) { FileInfo file = new FileInfo(srcFilePath); if (file.Directory != null) file.Directory.Create(); Stream sw; sw = file.Create(); sw.Write(bytes, 0, bytes.Length); sw.Close(); sw.Dispose(); } /// /// 重命名 /// /// /// public static string RenameFile(string srcFilePath, string newName) { if (string.IsNullOrEmpty(newName)) return null; var fileInfo = new FileInfo(srcFilePath); var newPath = Path.Combine(fileInfo.DirectoryName, newName) + fileInfo.Extension; Debug.Log(newPath); fileInfo.MoveTo(newPath); return newPath; } /// /// 弃用 /// /// /// [Obsolete("弃用,不允许重名")] public static void RenameFiles(string srcFilePath, string newName) { if (string.IsNullOrEmpty(newName)) return; var directoryInfo = new DirectoryInfo(srcFilePath); var fileInfos = directoryInfo.GetFiles(); foreach (var fileInfo in fileInfos) { fileInfo.MoveTo(Path.Combine(directoryInfo.FullName, newName) + fileInfo.Extension); } } /// /// 准确说这个应该叫拷贝,因为这个是复制在同一个文件夹下 /// /// public static void CopyFile(string srcFilePath) { var info = new FileInfo(srcFilePath); var cop = info.Name.Split(".")[0] + " cop"; string newFilePath = Path.Combine(info.Directory.FullName, cop) + info.Extension; File.Copy(srcFilePath, newFilePath, true); } /// /// 移动文件到指定位置 /// /// /// public static void MoveFile(string srcFilePath, string newFilePath) { var info = new FileInfo(srcFilePath); info.MoveTo(newFilePath); } /// /// 删除文件(不可逆,慎用) /// /// public static void DeleteFile(string srcFilePath) { FileInfo file = new FileInfo(srcFilePath); file.Delete(); } #endregion #region Directory /// /// 不存在文件夹就创建 /// /// public static void CreateDirectory(string srcRootPath) { if (!Directory.Exists(srcRootPath)) Directory.CreateDirectory(srcRootPath); } /// /// 删除文件夹(递归删除所有子文件) /// /// public static void DeleteDirectory(string srcRootPath) { if (!Directory.Exists(srcRootPath)) return; DirectoryInfo dir = new DirectoryInfo(srcRootPath); dir.Delete(true); } /// /// 移动文件夹到指定位置(包括所有子文件) /// /// /// public static void MoveDirectoryWithFiles(string srcRootPath, string destRootPath) { CopyFolderWithFiles(srcRootPath, destRootPath); DeleteDirectory(srcRootPath); } /// /// 重命名文件夹 /// /// /// /// public static string RenameDirectory(string srcFilePath, string newName) { if (string.IsNullOrEmpty(newName)) return null; var directoryInfo = new DirectoryInfo(srcFilePath); var newPath = Path.Combine(directoryInfo.Parent.FullName, newName); Debug.Log(newPath); directoryInfo.MoveTo(newPath); return newPath; } /// /// 复制文件夹到指定位置(包括所有子文件) /// /// /// /// public static void CopyFolderWithFiles(string srcRootPath, string destRootPath) { if (!Directory.Exists(srcRootPath)) throw new Win32Exception($"srcRootPath : {srcRootPath} not exists!"); if (!Directory.Exists(destRootPath)) throw new Win32Exception($"destRootPath : {destRootPath} not exists!"); if (Path.GetFullPath(srcRootPath) == Path.GetFullPath(destRootPath)) return; srcRootPath = srcRootPath[^1] != Path.DirectorySeparatorChar ? srcRootPath + Path.DirectorySeparatorChar : srcRootPath; destRootPath = destRootPath[^1] != Path.DirectorySeparatorChar ? destRootPath + Path.DirectorySeparatorChar : destRootPath; foreach (var srcFileFullName in Directory.GetFiles(srcRootPath, "*.*", SearchOption.TopDirectoryOnly)) { Task.Run(() => { try { var srcFileInfo = new FileInfo(srcFileFullName); var srcFileNameBaseRoot = srcFileInfo.FullName.Replace(srcRootPath, string.Empty); var destFileFullName = Path.Combine(destRootPath, srcFileNameBaseRoot); var destDirectoryInfo = new FileInfo(destFileFullName).Directory; if (destDirectoryInfo == null) { Debug.LogError($"destDirectoryInfo is null where path is :{destFileFullName}"); return; } if (!destDirectoryInfo.Exists) { destDirectoryInfo.Create(); } File.Copy(srcFileFullName, destFileFullName, true); Debug.Log( $"copy file: {Path.GetFullPath(srcFileFullName)} to {Path.GetFullPath(destFileFullName)}!"); } catch (Exception e) { Debug.LogError(e); throw; } }); } foreach (var srcDirectoryFullName in Directory.GetDirectories(srcRootPath, "*", SearchOption.TopDirectoryOnly)) { var srcDirectoryBaseRootPath = srcDirectoryFullName.Replace(srcRootPath, String.Empty); CopyFolderWithFiles(srcDirectoryFullName, Path.Combine(destRootPath, srcDirectoryBaseRootPath)); } } #endregion } }