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

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

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

/***

    目录工具类

    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;
        }
    }
}

http://www.kler.cn/news/335569.html

相关文章:

  • 大数据新视界 --大数据大厂之 Ibis:独特架构赋能大数据分析高级抽象层
  • Ubuntu 通过 Docker 搭建 GitLab
  • 数据结构与算法篇((原/反/补)码 进制)
  • 使用axios封装AJAX
  • 计算机毕业设计 基于Python的社交音乐分享平台的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
  • DOS 命令学习笔记
  • Apollo配置中心实战
  • PHP 基础语法详解
  • Java项目实战II基于Java+Spring Boot+MySQL的大创管理系统(源码+数据库+文档)
  • Spring MVC__@RequestMapping注解、获取请求参数、域对象共享数据、视图、Restful
  • 【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
  • 24-10-2-读书笔记(二十二)-《契诃夫文集》(一)上([俄] 契诃夫 [译] 汝龙)啊!真想生活。
  • mac M2安装单机版 MongoDB 7.x
  • Spring Cloud面试题收集
  • Python知识点:在Python编程中,如何使用Gensim进行主题建模
  • 10月5日刷题记录
  • 【重学 MySQL】四十五、数据库的创建、修改与删除
  • Unity Shader Graph基础包200+节点及术语解释
  • webstorm的缩进设置(过度缩进解释)
  • Pikachu-Sql Inject-insert/update/delete注入