此文记录的是目录工具类。
/*** 目录工具类 Austin Liu 刘恒辉 Project Manager and Software Designer E-Mail: lzhdim@163.com Blog: http://lzhdim.cnblogs.com Date: 2024-01-15 15:18:00 ***/ namespace Lzhdim.LPF.Utility { using System.IO; /// <summary> /// The Object End of Directory /// </summary> public static class DirUtil { /// <summary> /// 复制指定目录到另一个目录(包括子目录和所有文件) /// </summary> /// <param name="sourceDir">源目录</param> /// <param name="targetDir">目标目录</param> /// <exception cref="DirectoryNotFoundException"></exception> public static void CopyDirectory(string sourceDir, string targetDir) { DirectoryInfo dir = new DirectoryInfo(sourceDir); DirectoryInfo[] dirs = dir.GetDirectories(); // If the source directory does not exist, throw an exception. if (!dir.Exists) { throw new DirectoryNotFoundException($"Source directory does not exist or could not be found: {sourceDir}"); } // If the destination directory does not exist, create it. if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } // Get the files in the directory and copy them to the new location. FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string tempPath = Path.Combine(targetDir, file.Name); file.CopyTo(tempPath, false); } // If copying subdirectories, copy them and their contents to the new location. foreach (DirectoryInfo subdir in dirs) { string tempPath = Path.Combine(targetDir, subdir.Name); CopyDirectory(subdir.FullName, tempPath); } } /// <summary> /// Create a dir /// </summary> /// <param name="dir">dir which need to create</param> /// <returns>true dir is create;false dir is create error</returns> public static bool CreateDir(string dir) { if (!Directory.Exists(dir)) { try { Directory.CreateDirectory(dir); return true; } catch { } return false; } return true; } /// <summary> /// Delete a dir /// </summary> /// <param name="dir">dir which need to delete</param> /// <returns>true dir is delete;false dir is delete error</returns> public static bool DeleteDir(string dir) { if (Directory.Exists(dir)) { try { Directory.Delete(dir); return true; } catch { } return false; } return false; } /// <summary> /// Check if dir is exist /// </summary> /// <param name="dir">dir which need to check</param> /// <returns>true dir is exist;false dir is not exist</returns> public static bool DirIsExist(string dir) { return Directory.Exists(dir); } /// <summary> /// rename a dir /// </summary> /// <param name="srcDir">dir which need to rename</param> /// <param name="desDir">the renamed dir name</param> /// <returns>true rename is done;false rename is error</returns> public static bool RenameDir(string srcDir, string desDir) { try { Directory.Move(srcDir, desDir); return true; } catch { } return false; } } }
![]() |
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |
分类:
【153】C#小函数类推荐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2023-10-05 人品 - 我的闪存