using System; using System.Collections.Generic; using System.IO; namespace FilesClass { public class FileOperate { /// <summary> /// 判断文件是否存在 /// </summary> /// <param name="path">文件路径</param> /// <returns>判断文件是否存在</returns> public static bool IsExistsFile(string path) { return File.Exists(path); } /// <summary> /// 判断文件夹是否存在 /// </summary> /// <param name="path">文件夹路径</param> /// <returns>判断文件夹是否存在</returns> public static bool IsExistsDir(string path) { return Directory.Exists(path); } /// <summary> /// /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool CreateDir(string path) { try { Directory.CreateDirectory(path); return true; } catch (Exception ex) { throw new Exception("创建文件夹失败,路径是>>" + path + " ,失败原因>>" + ex.Message); } } /// <summary> /// 删除文件 /// </summary> /// <param name="path">文件路径</param> public static void DeleteFile(string path) { if (IsExistsFile(path)) { File.Delete(path); } } /// <summary> /// 拷贝一个文件夹里面的东西 到另外一个文件夹 /// </summary> /// <param name="sourcePath"> 源文件夹 </param> /// <param name="targetPath"> 目标文件夹 </param> public static void CopyFolder(string sourcePath, string targetPath) { if (IsExistsDir(sourcePath)) { if (!IsExistsDir(targetPath)) { CreateDir(targetPath); } //获得源文件下所有文件 List<string> filesList = new List<string>(Directory.GetFiles(sourcePath)); filesList.ForEach(file => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(file)}); File.Copy(file, targetDir); }); //获取源文件下所有的目录文件 List<string> foldersList = new List<string>(Directory.GetDirectories(sourcePath)); foldersList.ForEach(folder => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(folder)}); CopyFolder(folder, targetDir); }); } else { throw new DirectoryNotFoundException("源目录不存在"); } } /// <summary> /// 移动文件夹到另外一个文件夹 /// </summary> /// <param name="sourcePath"> 源文件夹 </param> /// <param name="targetPath"> 目标文件夹 </param> public static void MoveFolder(string sourcePath, string targetPath) { if (IsExistsDir(sourcePath)) { if (IsExistsDir(targetPath)) { CreateDir(targetPath); } List<string> filesList = new List<string>(Directory.GetFiles(sourcePath)); filesList.ForEach(file => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(file)}); File.Copy(file, targetDir); }); List<string> folderList = new List<string>(Directory.GetDirectories(sourcePath)); folderList.ForEach(folder => { string targetDir = Path.Combine(new string[] {targetPath, Path.GetDirectoryName(folder)}); MoveFolder(folder, targetDir); }); } else { throw new DirectoryNotFoundException("源目录不存在"); } } /// <summary> /// 删除文件夹 /// </summary> /// <param name="dirPath">目标文件夹</param> public static void DeleteFolder(string dirPath) { if (IsExistsDir(dirPath)) { foreach (string content in Directory.GetFileSystemEntries(dirPath)) { if (IsExistsDir(content)) { Directory.Delete(content, true); } else if (IsExistsFile(content)) { DeleteFile(content); } } } } /// <summary> /// 删除文件夹 删除文件夹然后新建 /// </summary> /// <param name="dirPath">目标文件夹</param> public static void DeleteFolderEx(string dirPath) { if (IsExistsDir(dirPath)) { Directory.Delete(dirPath, true); Directory.CreateDirectory(dirPath); } } /// <summary> /// 重命名文件夹 /// </summary> /// <param name="sourcePath"> 源文件夹 </param> /// <param name="targetPath"> 目标文件夹 </param> public static void RenameFolder(string sourcePath, string targetPath) { CopyFolder(sourcePath, targetPath); DeleteFolder(sourcePath); } } }