zoukankan      html  css  js  c++  java
  • 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);
                }
            }
        }
    }
  • 相关阅读:
    性能测试理论知识
    接口测试笔试题
    测试计划与测试报告
    java基础面试题
    软件测试人员必备的linux命令
    tomcat各目录(文件)作用
    常见的面试题
    LoadRunner中怎么设置密码参数化与用户名关联
    数据库索引总结(二)
    数据库索引总结(一)
  • 原文地址:https://www.cnblogs.com/password1/p/5870693.html
Copyright © 2011-2022 走看看