目录工具类 - C#小函数类推荐

news/2024/10/11 8:44:50/

       此文记录的是目录工具类。

/***目录工具类Austin Liu 刘恒辉Project Manager and Software DesignerE-Mail: lzhdim@163.comBlog:   http://lzhdim.cnblogs.comDate:   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;}}
}

http://www.ppmy.cn/news/1537360.html

相关文章

【Docker从入门到进阶】06.常见问题与解决方案 07.总结与资源

6. 常见问题与解决方案 在使用Docker进行开发和部署过程中&#xff0c;可能会遇到各种问题。以下是一些常见问题及其解决方案&#xff1a; 容器启动失败和调试 在使用 Docker 时&#xff0c;容器启动失败或立即退出可能会导致一定的困扰&#xff0c;以下是进一步深入解决该问…

Visual Studio 2022安装(含重生版)

前言&#xff1a; 昨天调试代码的时候发现程序怎么都运行不了&#xff0c;错误显示无法找到文件啊啊啊&#xff0c;能力有限&#xff0c;找不出错误源&#xff0c;然后就狠心删掉所有相关文件来“重新开始”&#xff01; 正文&#xff1a; 1.官网下载&#xff08;内定中文版…

鸿蒙next开发者第一课02.DevEcoStudio的使用-习题

【习题】DevEco Studio的使用 通过/及格分80/ 满分100 判断题 1. 如果代码中涉及到一些网络、数据库、传感器等功能的开发&#xff0c;均可使用预览器进行预览。F 正确(True)错误(False) 预览器不能进行传感器等特殊功能的开发,需要使用真机开发 2. module.json5文件中的…

实用Linux脚本

MySQL备份 #!/bin/bashset -eUSER"backup" PASSWORD"backup" # 数据库数据目录 # DATA_DIR"/data/mysql" BIN_INDEX$DATA_DIR"/mysql-bin.index" # 备份目录 # BACKUP_DIR"/data/backup/mysql" BACKUP_LOG"/var/log/m…

微软最新 Office 办公软件2025下载 – Microsoft 365 正版优惠订阅

​ 以前 Office 365 是微软打造的「办公软件订阅」服务。订阅后&#xff0c;用户可以在多个平台使用Word、Excel、PowerPoint、OneDrive云存储网盘等正版办公应用。 微软希望这种订阅方式能够推广到更多的产品和用户&#xff0c;于是决定将 Office 365 升级为全新的「Microsoft…

PFC和LLC的本质和为什么要用PFC和LLC电路原因

我们可以用电感和电容的特性,以及电压和电流之间的不同步原理来解释PFC(功率因数校正)和LLC(谐振变换器)。 电感和电容的基本概念 电感(Inductor): 电感是一种储存电能的组件。它的电流变化比较慢,电流在电感中延迟,而电压变化得比较快。可以把电感想象成一个“滞后…

IT运维如果转行能干什么?

今天根据我在前司的工作经验&#xff0c;来分析一下桌面运维工程师&#xff0c;如果想转行&#xff0c;或者进一步发展能干什么。 1.机房运维。为什么上来就是聊机房运维尼&#xff0c;因为在前司时候每天最重要的工作就是检查机房&#xff0c;检查服务器&#xff0c;交换机&a…

c++中的final说明符

final是c11引入的说明符&#xff0c;可以修饰在类或者类成员函数尾部。 final修饰类时&#xff0c;表示该类不能被继承。 class A final { }; 上例表明&#xff0c;类A是一个不能被继承的类。 class A { public:virtual void bar(); };class B final: public A { public:voi…