2013-10-31 83 views
1

我想在我的文件枚舉中過濾出路徑C:\$Recycle.bin。我怎樣才能做到這一點?SearchOption.AllDirectories過濾器

var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).OrderBy(p => p).ToList(); 

當我執行上述,我得到下面的錯誤。

其他信息:訪問路徑'C:\ $ Recycle.Bin \ S-1-5-21-1600837348-2291285090-976156579-500'被拒絕。

我也想計算每個文件的md5。我有:

    var mainDirectory = new DirectoryInfo("\\"); 

        var files = GetDirectories(mainDirectory); 
        List<string> drives = new List<string>(); 

        foreach (var file in files) 
        { 
         //Console.WriteLine(file.Name); 
         drives.Add(mainDirectory + file.Name); 
        } 

        MD5 md5 = MD5.Create(); 

        foreach (string file in drives) 
        { 
         // hash path 
         string relativePath = file.Substring("\\".Length + 1); 
         byte[] pathBytes = Encoding.UTF8.GetBytes(relativePath.ToLower()); 
         md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0); 

         // hash contents 
         try 
         { 
          byte[] contentBytes = File.ReadAllBytes(file); 

          md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0); 
          md5.TransformFinalBlock(contentBytes, 0, contentBytes.Length); 
         } 
         catch(UnauthorizedAccessException) 
         { 
          continue; 
         } 
         catch 
         { 
          continue; 
         } 

         Console.WriteLine(BitConverter.ToString(md5.Hash).Replace("-", "").ToLower()); 
        } 
        Console.ReadKey(); 
+0

恐怕這可能是一個棘手的問題。我認爲它是框架中的一個錯誤。 :( – Gabe

+0

你不能!唯一的選擇是自己寫一個遞歸函數 – Silvermind

+0

可能的重複[在搜索文件時訪問被拒絕的錯誤](http://stackoverflow.com/questions/10458862/access-denied-error -in-while-searching-for-files) – Gabe

回答

0

以下內容可以爲你做,但它是一種快速和骯髒的方式,因爲它不處理任何異常。我沒有考慮到任何可讀性,並沒有完全測試。

static void Main(string[] args) 
    { 
     var mainDirectory = new DirectoryInfo("C:\\"); 
     var files = GetFiles(mainDirectory, "."); 
     foreach (var file in files) 
     { 
      Console.WriteLine(file.Name); 
     } 
     Console.ReadKey(); 
    } 

    static IEnumerable<DirectoryInfo> GetDirectories(DirectoryInfo parentDirectory) 
    { 
     DirectoryInfo[] childDirectories = null; 
     try 
     { 
      childDirectories = parentDirectory.GetDirectories(); 
     } 
     catch (Exception) 
     { 

     } 
     yield return parentDirectory; 
     if (childDirectories != null) 
     { 
      foreach (var childDirectory in childDirectories) 
      { 
       var childDirectories2 = GetDirectories(childDirectory); 
       foreach (var childDirectory2 in childDirectories2) 
       { 
        yield return childDirectory2; 
       } 
      } 
     } 
    } 

    static IEnumerable<FileInfo> GetFiles(DirectoryInfo parentDirectory, 
              string searchPattern) 
    { 
     var directories = GetDirectories(parentDirectory); 
     foreach (var directory in directories) 
     { 
      FileInfo[] files = null; 
      try 
      { 
       files = directory.GetFiles(searchPattern); 
      } 
      catch (Exception) 
      { 
      } 
      if (files != null) 
      { 
       foreach (var file in files) 
       { 
        yield return file; 
       } 
      } 
     } 
    } 
+0

運行良好,但我也想計算每個文件的md5。 – user2216791

+0

@ user2216791你可以在迭代每個文件的foreach時做到這一點''static void Main''方法的循環 – Silvermind

+0

我看到了,但我認爲我正在計算md5的方式,它不喜歡這樣一個事實,即我沒有將它填滿整個路徑,而是我餵食它只是文件名。 (我認爲) – user2216791