代码改变世界

c# 文件/文件夹操作

2016-09-14 09:33  newbirth  阅读(263)  评论(0编辑  收藏  举报
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);
            }
        }
    }
}