• [转]C# 获取指定目录下所有文件信息、移动目录、拷贝目录


    原文:http://blog.csdn.net/vchao13/article/details/6200255

    1、获取指定目录下所有文件信息

    1. /// <summary>  
    2.       /// 返回指定目录下所有文件信息  
    3.       /// </summary>  
    4.       /// <param name="strDirectory">目录字符串</param>  
    5.       /// <returns></returns>  
    6.       public List<FileInfo> GetAllFilesInDirectory(string strDirectory)  
    7.       {  
    8.           List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息  
    9.           DirectoryInfo directory = new DirectoryInfo(strDirectory);  
    10.           DirectoryInfo[] directoryArray = directory.GetDirectories();  
    11.           FileInfo[] fileInfoArray = directory.GetFiles();  
    12.           if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray);  
    13.           foreach (DirectoryInfo _directoryInfo in directoryArray)  
    14.           {  
    15.               DirectoryInfo directoryA = new DirectoryInfo(_directoryInfo.FullName);  
    16.               DirectoryInfo[] directoryArrayA = directoryA.GetDirectories();  
    17.               FileInfo[] fileInfoArrayA = directoryA.GetFiles();  
    18.               if (fileInfoArrayA.Length > 0) listFiles.AddRange(fileInfoArrayA);  
    19.               GetAllFilesInDirectory(_directoryInfo.FullName);//递归遍历  
    20.           }  
    21.           return listFiles;  
    22.       }  

    2、移动此目录到指定目录

    1. /// <summary>  
    2.         /// 从一个目录将其内容移动到另一目录  
    3.         /// </summary>  
    4.         /// <param name="directorySource">源目录</param>  
    5.         /// <param name="directoryTarget">目的目录</param>  
    6.         private void  MoveFolderTo(string directorySource, string directoryTarget)  
    7.         {  
    8.             //检查是否存在目的目录  
    9.             if (!Directory.Exists(directoryTarget))  
    10.             {  
    11.                 Directory.CreateDirectory(directoryTarget);                 
    12.             }  
    13.             //先来移动文件  
    14.             DirectoryInfo directoryInfo = new DirectoryInfo(directorySource);  
    15.             FileInfo[] files = directoryInfo.GetFiles();  
    16.             //移动所有文件  
    17.             foreach (FileInfo file in files)  
    18.             {                 
    19.                 //如果自身文件在运行,不能直接覆盖,需要重命名之后再移动  
    20.                 if (File.Exists(Path.Combine(directoryTarget, file.Name)))  
    21.                 {  
    22.                     if (File.Exists(Path.Combine(directoryTarget, file.Name + ".bak")))  
    23.                     {  
    24.                         File.Delete(Path.Combine(directoryTarget, file.Name + ".bak"));  
    25.                     }  
    26.                     File.Move(Path.Combine(directoryTarget, file.Name), Path.Combine(directoryTarget, file.Name + ".bak"));  
    27.                      
    28.                 }  
    29.                 file.MoveTo(Path.Combine(directoryTarget, file.Name));  
    30.                  
    31.             }  
    32.             //最后移动目录  
    33.             DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();  
    34.             foreach (DirectoryInfo dir in directoryInfoArray)  
    35.             {  
    36.                 MoveFolderTo(Path.Combine(directorySource, dir.Name), Path.Combine(directoryTarget, dir.Name));  
    37.             }              
    38.         }         

    3、拷贝此目录到指定目录

    1. /// <summary>  
    2.        /// 从一个目录将其内容复制到另一目录  
    3.        /// </summary>  
    4.        /// <param name="directorySource">源目录</param>  
    5.        /// <param name="directoryTarget">目的目录</param>  
    6.        static void CopyFolderTo(string directorySource, string directoryTarget)  
    7.        {  
    8.            //检查是否存在目的目录  
    9.            if (!Directory.Exists(directoryTarget))  
    10.            {  
    11.                Directory.CreateDirectory(directoryTarget);  
    12.            }  
    13.            //先来复制文件  
    14.            DirectoryInfo directoryInfo = new DirectoryInfo(directorySource);  
    15.            FileInfo[] files = directoryInfo.GetFiles();  
    16.            //复制所有文件  
    17.            foreach (FileInfo file in files)  
    18.            {                 
    19.                file.CopyTo (Path.Combine(directoryTarget, file.Name));  
    20.            }  
    21.            //最后复制目录  
    22.            DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();  
    23.            foreach (DirectoryInfo dir in directoryInfoArray)  
    24.            {  
    25.                CopyFolderTo(Path.Combine(directorySource, dir.Name), Path.Combine(directoryTarget, dir.Name));  
    26.            }  
    27.   
    28.        }    
  • 相关阅读:
    如何使用jackson美化输出json/xml
    使用Jackson在Java中处理JSON
    用 Jackson 来处理 JSON
    writeValueAsString封装成工具类
    周鸿袆:360回归是出于国家安全考虑(硬件有硬件独特的规律,硬件不可能有很高的利润,核心的价值还是硬件背后承载的软件和云端的服务)
    Unicode 7.0.1中文支持非常好
    六个编程范型将改变你对编程的看法(好多奇怪的语言和奇怪的想法)
    delphi多版本安装方法
    UAC就不能一次添加、永久信任吗?
    数学符号及读法大全
  • 原文地址:https://www.cnblogs.com/lisengl/p/3336415.html
Copyright © 2020-2023  润新知