C#遍历文件夹及其子目录

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using System.Security.AccessControl;  
  6. using System.Text;  
  7.   
  8. namespace ConsoleApplication1  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Console.WriteLine("STT");  
  15.             string str = @"E:\";  
  16.             if (!str.EndsWith("\\"))   
  17.             {  
  18.                 str += "\\";  
  19.             }  
  20.             IList<FileInfo> lst = GetFiles(str);  
  21.             if (!Directory.Exists(str))  
  22.             {  
  23.                 try  
  24.                 {  
  25.                     Directory.CreateDirectory(str);  
  26.                 }  
  27.                 catch(Exception e)  
  28.                 {  
  29.                     Console.WriteLine(e.Message);  
  30.                     Console.ReadKey();  
  31.                     return;  
  32.                 }  
  33.             }  
  34.             if (File.Exists(str + "test.txt"))  
  35.             {  
  36.                 File.Delete(str + "test.txt");  
  37.             }  
  38.             FileInfo file = new FileInfo(str + "test.txt");  
  39.             if (!file.Directory.Exists)  
  40.             {  
  41.                 Directory.CreateDirectory(file.DirectoryName);  
  42.             }  
  43.             using (StreamWriter outFileWriter = new StreamWriter(str + "test.txt"false, Encoding.UTF8))  
  44.             {  
  45.                 StringBuilder sb = new StringBuilder();  
  46.                 foreach (FileInfo item in lst)  
  47.                 {  
  48.                     sb.Append("\"");  
  49.                     sb.Append(item.FullName);  
  50.                     sb.Append("\"");  
  51.                     sb.Append(",");  
  52.                     sb.Append("\r\n");  
  53.                 }  
  54.                 sb.Remove(sb.Length - 2, 2);  
  55.                 outFileWriter.WriteLine(sb.ToString());  
  56.                 outFileWriter.Flush();  
  57.                 outFileWriter.Close();  
  58.             }  
  59.             Console.WriteLine("END");  
  60.             Console.ReadKey();  
  61.         }  
  62.   
  63.         private static void GetDirectorys(string strPath, ref List<string> lstDirect)  
  64.         {  
  65.             DirectoryInfo diFliles = new DirectoryInfo(strPath);  
  66.             DirectoryInfo[] diArr = diFliles.GetDirectories();  
  67.             //DirectorySecurity directorySecurity = null;  
  68.             foreach (DirectoryInfo di in diArr)  
  69.             {  
  70.                 try  
  71.                 {  
  72.                     //directorySecurity = new DirectorySecurity(di.FullName, AccessControlSections.Access);  
  73.                     //if (!directorySecurity.AreAccessRulesProtected)  
  74.                     //{  
  75.                     lstDirect.Add(di.FullName);  
  76.                     GetDirectorys(di.FullName, ref lstDirect);  
  77.                     //}  
  78.                 }  
  79.                 catch   
  80.                 {  
  81.                     continue;  
  82.                 }  
  83.             }  
  84.         }  
  85.         /// <summary>  
  86.         /// 遍历当前目录及子目录  
  87.         /// </summary>  
  88.         /// <param name="strPath">文件路径</param>  
  89.         /// <returns>所有文件</returns>  
  90.         private static IList<FileInfo> GetFiles(string strPath)  
  91.         {  
  92.             List<FileInfo> lstFiles = new List<FileInfo>();  
  93.             List<string> lstDirect = new List<string>();  
  94.             lstDirect.Add(strPath);  
  95.             DirectoryInfo diFliles = null;  
  96.             GetDirectorys(strPath, ref lstDirect);  
  97.             foreach (string str in lstDirect)  
  98.             {  
  99.                 try  
  100.                 {  
  101.                     diFliles = new DirectoryInfo(str);  
  102.                     lstFiles.AddRange(diFliles.GetFiles());  
  103.                 }  
  104.                 catch   
  105.                 {  
  106.                     continue;  
  107.                 }  
  108.             }  
  109.             return lstFiles;  
  110.         }  
  111.     }  
  112. }  

[csharp]  view plain copy
  1. <pre name="code" class="csharp"></pre>  
  2. <pre></pre>  
  3. <pre name="code" class="csharp"></pre><pre name="code" class="csharp"></pre><pre name="code" class="csharp"></pre>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值