1. 程式人生 > >文件操作類

文件操作類

sha reac files combine ren class new ner 所有

using System;
using System.Collections.Generic;
using System.IO;

namespace FilesClass
{
    public class FileOperate
    {
        /// <summary>
        /// 判斷文件是否存在
        /// </summary>
        /// <param name="path">文件路徑</param>
        /// <returns>判斷文件是否存在</returns>
        public static bool IsExistsFile(string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// 判斷文件夾是否存在
        /// </summary>
        /// <param name="path">文件夾路徑</param>
        /// <returns>判斷文件夾是否存在</returns>
        public static bool IsExistsDir(string path)
        {
            return Directory.Exists(path);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool CreateDir(string path)
        {
            try
            {
                Directory.CreateDirectory(path);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("創建文件夾失敗,路徑是>>" + path + " ,失敗原因>>" + ex.Message);
            }
        }

        /// <summary>
        /// 刪除文件
        /// </summary>
        /// <param name="path">文件路徑</param>
        public static void DeleteFile(string path)
        {
            if (IsExistsFile(path))
            {
                File.Delete(path);
            }
        }

        /// <summary>
        /// 拷貝一個文件夾裏面的東西 到另外一個文件夾
        /// </summary>
        /// <param name="sourcePath"> 源文件夾 </param>
        /// <param name="targetPath"> 目標文件夾 </param>
        public static void CopyFolder(string sourcePath, string targetPath)
        {
            if (IsExistsDir(sourcePath))
            {
                if (!IsExistsDir(targetPath))
                {
                    CreateDir(targetPath);
                }

                //獲得源文件下所有文件
                List<string> filesList = new List<string>(Directory.GetFiles(sourcePath));
                filesList.ForEach(file =>
                {
                    string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(file)});
                    File.Copy(file, targetDir);
                });

                //獲取源文件下所有的目錄文件
                List<string> foldersList = new List<string>(Directory.GetDirectories(sourcePath));
                foldersList.ForEach(folder =>
                {
                    string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(folder)});
                    CopyFolder(folder, targetDir);
                });
            }
            else
            {
                throw new DirectoryNotFoundException("源目錄不存在");
            }
        }

        /// <summary>
        /// 移動文件夾到另外一個文件夾
        /// </summary>
        /// <param name="sourcePath"> 源文件夾 </param>
        /// <param name="targetPath"> 目標文件夾 </param>
        public static void MoveFolder(string sourcePath, string targetPath)
        {
            if (IsExistsDir(sourcePath))
            {
                if (IsExistsDir(targetPath))
                {
                    CreateDir(targetPath);
                }

                List<string> filesList = new List<string>(Directory.GetFiles(sourcePath));

                filesList.ForEach(file =>
                {
                    string targetDir = Path.Combine(new string[] {targetPath, Path.GetFileName(file)});
                    File.Copy(file, targetDir);
                });

                List<string> folderList = new List<string>(Directory.GetDirectories(sourcePath));
                folderList.ForEach(folder =>
                {
                    string targetDir = Path.Combine(new string[] {targetPath, Path.GetDirectoryName(folder)});
                    MoveFolder(folder, targetDir);
                });
            }
            else
            {
                throw new DirectoryNotFoundException("源目錄不存在");
            }
        }

        /// <summary>
        /// 刪除文件夾
        /// </summary>
        /// <param name="dirPath">目標文件夾</param>
        public static void DeleteFolder(string dirPath)
        {
            if (IsExistsDir(dirPath))
            {
                foreach (string content in Directory.GetFileSystemEntries(dirPath))
                {
                    if (IsExistsDir(content))
                    {
                        Directory.Delete(content, true);
                    }
                    else if (IsExistsFile(content))
                    {
                        DeleteFile(content);
                    }
                }
            }
        }

        /// <summary>
        /// 刪除文件夾  刪除文件夾然後新建
        /// </summary>
        /// <param name="dirPath">目標文件夾</param>
        public static void DeleteFolderEx(string dirPath)
        {
            if (IsExistsDir(dirPath))
            {
                Directory.Delete(dirPath, true);
                Directory.CreateDirectory(dirPath);
            }
        }

        /// <summary>
        /// 重命名文件夾
        /// </summary>
        /// <param name="sourcePath"> 源文件夾 </param>
        /// <param name="targetPath"> 目標文件夾 </param>
        public static void RenameFolder(string sourcePath, string targetPath)
        {
            CopyFolder(sourcePath, targetPath);
            DeleteFolder(sourcePath);
        }
    }
}

  

文件操作類