2012-03-10 12 views
0

私はディレクトリを使用する場合に発生するアクセス拒否の例外を無視してサブディレクトリを含むフォルダ/ドライブからすべてのファイルを表示しようとしているが拒否されましたAccesのを避けてファイルリストを解析します。 GetFiles()メソッド。バックグラウンドワーカーは、再帰的に例外

私はMethod.Theの問題は方法は、それ自体への再帰呼び出しを含むことである持っている、私はrecursively.How iは背景の仕事として、このメソッドを実装することができ、バックグラウンドワーカーを呼び出すことはできません。

static void WalkDirectoryTree(System.IO.DirectoryInfo root) 
{ 
    System.IO.FileInfo[] files = null; 
    System.IO.DirectoryInfo[] subDirs = null; 

    // First, process all the files directly under this folder 
    try 
    { 
     files = root.GetFiles("*.*"); 
    } 
    // This is thrown if even one of the files requires permissions greater 
    // than the application provides. 
    catch (UnauthorizedAccessException e) 
    { 
     // This code just writes out the message and continues to recurse. 
     // You may decide to do something different here. For example, you 
     // can try to elevate your privileges and access the file again. 
     log.Add(e.Message); 
    } 

    catch (System.IO.DirectoryNotFoundException e) 
    { 
     Console.WriteLine(e.Message); 
    } 

    if (files != null) 
    { 
     foreach (System.IO.FileInfo fi in files) 
     { 
      // In this example, we only access the existing FileInfo object. If we 
      // want to open, delete or modify the file, then 
      // a try-catch block is required here to handle the case 
      // where the file has been deleted since the call to TraverseTree(). 
      Console.WriteLine(fi.FullName); 
     } 

     // Now find all the subdirectories under this directory. 
     subDirs = root.GetDirectories(); 

     foreach (System.IO.DirectoryInfo dirInfo in subDirs) 
     { 
      // Resursive call for each subdirectory. 
      WalkDirectoryTree(dirInfo); 
     } 
    }    
} 

}

答えて

1

非常に簡単:ちょうどあなたの背景労働者の体-メソッド内でこのメソッドを呼び出します。

+0

文句を言わない、それはGUI冷凍を行います。背景労働者が何のためにあるのかだ – techno

+0

はそうではありませんか? – Carsten