• c# 文件/文件夹操作


    using System;
    using System.IO;
    
    namespace HuaTong.General.Utility
    {
        /// <summary>
        /// 文件夹/文件操作
        /// </summary>
        public static class DirFileHelper
        {
            /// <summary>
            /// 获取所有子目录列表,包含嵌套的子目录
            /// </summary>     
            public static string[] GetDirectories(string directoryPath, string searchName)
            {
                return Directory.GetDirectories(directoryPath, searchName, SearchOption.AllDirectories);
            }
            /// <summary>
            /// 获取所有文件列表,包含嵌套的子目录
            /// </summary>
            public static string[] GetFileNames(string directoryPath, string searchName)
            {
                return Directory.GetFiles(directoryPath, searchName, SearchOption.AllDirectories);
            }
    
            /// <summary>
            /// 检测是否存在指定的文件,包含嵌套的子目录
            /// </summary>       
            public static bool ContainsFile(string directoryPath, string searchName)
            {
                //获取指定的文件列表
                string[] fileNames = GetFileNames(directoryPath, searchName);
                //判断指定文件是否存在
                return !(fileNames.Length == 0);
            }
            /// <summary>
            /// 检测是否存在指定的文件,包含嵌套的子目录
            /// </summary>
            public static bool ContainsDirectory(string directoryPath, string searchName)
            {
                //获取指定的目录列表
                string[] dirNames = GetDirectories(directoryPath, searchName);
                //判断指定目录是否存在
                return !(dirNames.Length == 0);
            }
    
            /// <summary>
            /// 删除所有文件和子目录,包含嵌套的子目录
            /// </summary>
            public static void DeleteFiles(string directoryPath)
            {
                string[] subFiles = Directory.GetFiles(directoryPath);
                foreach (string fileName in subFiles)
                {
                    File.SetAttributes(fileName, FileAttributes.Normal);
                    File.Delete(fileName);
                }
    
                string[] subDirs = Directory.GetDirectories(directoryPath);
                foreach (string subDir in subDirs)
                {
                    DeleteFiles(subDir);
                    Directory.Delete(subDir);
                }
            }
    
            /// <summary>
            /// 剪切所有文件,包含嵌套的子目录,目标文件存在则覆盖
            /// </summary>
            public static void MoveFiles(string directoryPath, string targetDirectory)
            {
                if (!Directory.Exists(targetDirectory))
                    Directory.CreateDirectory(targetDirectory);
                string[] subFiles = Directory.GetFiles(directoryPath);
                foreach (string sourceFileName in subFiles)
                {
                    string targetFileName = Path.Combine(targetDirectory, Path.GetFileName(sourceFileName));
                    if (File.Exists(targetFileName))
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        File.Delete(targetFileName);
                    }
                    File.Move(sourceFileName, targetFileName);
                }
    
                string[] subDirs = Directory.GetDirectories(directoryPath);
                foreach (string sourceSubDir in subDirs)
                {
                    string targetSubDir = Path.Combine(targetDirectory, Path.GetFileName(sourceSubDir));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    MoveFiles(sourceSubDir, targetSubDir);
                    Directory.Delete(sourceSubDir);
                }
            }
    
            /// <summary>
            /// 复制所有文件,包含嵌套的子目录,目标文件存在则覆盖
            /// </summary>
            public static void CopyFiles(string directoryPath, string targetDirectory)
            {
                if (!Directory.Exists(targetDirectory))
                    Directory.CreateDirectory(targetDirectory);
                string[] subFiless = Directory.GetFiles(directoryPath);
                foreach (string sourceFileName in subFiless)
                {
                    string targetFileName = Path.Combine(targetDirectory, Path.GetFileName(sourceFileName));
    
                    if (File.Exists(targetFileName))
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                    }
                    File.Copy(sourceFileName, targetFileName);
                }
    
                string[] subDirs = Directory.GetDirectories(directoryPath);
                foreach (string sourceSubDir in subDirs)
                {
                    string targetSubDir = Path.Combine(targetDirectory, Path.GetFileName(sourceSubDir));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    CopyFiles(sourceSubDir, targetSubDir);
                }
            }
        }
    }
  • 相关阅读:
    【microstation CE二次开发】不打开microstation,以COM方式启动Microstation
    【microstation CE二次开发】环境搭建
    Node安装与卸载命令汇总
    Maven进行clean时报错,解决方法
    Django 报ckeditor/skins/moono/skin.js 404
    Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract
    精准测试系列分享之一:JaCoCo 企业级应用的优缺点分析
    Java 中常见的细粒度锁实现
    JVM 的运行时数据区域分布
    Java 细粒度锁续篇
  • 原文地址:https://www.cnblogs.com/password1/p/5870693.html
Copyright © 2020-2023  润新知