当前位置: 首页 > news >正文

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

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

/***目录工具类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.lryc.cn/news/454885.html

相关文章:

  • 速盾:如何判断高防服务器的防御是否真实?
  • MySQL连接查询:联合查询
  • Gitea 数据迁移
  • MySQL 绪论
  • 什么是 HTTP Get + Preflight 请求
  • (JAVA)开始熟悉 “二叉树” 的数据结构
  • 【Linux】Linux命令与操作详解(一)文件管理(文件命令)、用户与用户组管理(创建、删除用户/组)
  • Hadoop大数据入门——Hive-SQL语法大全
  • 个人开发主页
  • 思维+数论,CF 922C - Cave Painting
  • 如何下单PCB板和STM贴片服务- 嘉立创EDA
  • MySQL连接查询:外连接
  • 108页PPT丨OGSM战略规划框架:实现企业目标的系统化方法论
  • 文件查找与打包压缩,文件发送
  • sv标准研读第十二章-过程性编程语句
  • MySQL-联合查询
  • 突触可塑性与STDP:神经网络中的自我调整机制
  • 【小沐学GIS】QGIS导出OpenStreetMap数据(QuickOSM、OSM)
  • 推荐一款强大的书签管理工具,让你的网址不在落灰
  • Python 工具库每日推荐 【Matplotlib】
  • 在远程非桌面版Ubuntu中使用Qt5构建Hello World项目
  • netty之基础aio,bio,nio
  • 在找工作吗?给你一个AI虚拟面试官助力你提前准备面试
  • @KafkaListener注解中containerFactory属性的作用
  • 1006C简单题(计数式子的组合意义 + dp式子联立)
  • 千益畅行,旅游创业新模式的创新与发展
  • 单调栈day54|42. 接雨水(高频面试题)、84. 柱状图中最大的矩形、两道题思维导图的汇总与对比
  • 关于Excel将列号由字母改为数字
  • 曾黎第二次受邀巴黎时装周看秀 为新疆棉代言引人瞩目
  • No.6 笔记 | Linux操作系统基础:全面概览与核心要点