zoukankan      html  css  js  c++  java
  • 文件操作类

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

      

  • 相关阅读:
    IEnumerable<T>接口主要成员
    SQL LinqToSql Lambda QA
    css网页布局兼容性有哪些要点与诀窍
    【转】解决大量图片造成的页面延迟
    C# override,new 的区别+C# 接口的显示实现和隐示实现
    ap_invoice_distributions_all到xla_ae_lines
    Mysql从索引原理对SQL分析优化实战
    如何实现IM表情、图片、文件之间的通讯?
    五步教你如何使用k8s快速部署ES
    ClickHouse MergeTree引擎的简单介绍
  • 原文地址:https://www.cnblogs.com/kanekiken/p/7871952.html
Copyright © 2011-2022 走看看