2014-03-06 24 views
2

我需要在文件系統中找到一個名爲「GameData」的文件夾,它通常存儲在Program Files中,但如果不是,我想成爲能夠找到它,無論它在C驅動器中。需要一種在C#中搜索C盤中所有目錄的方法#

目前,我正在使用:

IEnumerable<string> list = Directory.GetDirectories(root, "GameData", SearchOption.AllDirectories); 

但它拋出一個UnauthorizedAccessException這是意料之中的,因爲它正試圖通過受保護的目錄導航。

有沒有什麼我可以添加到此來阻止它試圖訪問任何受保護的目錄?一個try/catch區塊是否允許在異常之後繼續搜索?

編輯

注:我不是在尋找一個特定的文件,只是叫GAMEDATA的文件夾,這樣我可以使用該位置爲.ZIP提取的輸出,或讀取的名字內的文件夾。

+0

也許有發現該文件夾的更好的辦法?請記住整個文件系統中可能有多個'GameData'目錄。並且搜索整個文件系統可能需要一段時間...您是否在安裝的應用程序中查找文件夾? – MrPaulch

+0

我有一個名爲「GameData」的文件夾,我在那裏保存了我的寵物魚和幾個關鍵重要文件的所有照片...希望你的軟件不會破壞它;-) – Ricibob

回答

2

你需要使用遞歸的方法,而不是AllDirectories。然後你可以跳過導致異常的目錄。

MSDN: Iterate Through a Directory Tree (C# Programming Guide)

「使用SearchOption.AllDirectories的缺點是,如果指定的根目錄下的子目錄中的任何一個引起DirectoryNotFoundExceptionUnauthorizedAccessException,整個方法失敗,也不返回目錄。當你使用也是如此GetFiles方法。如果你要處理具體的子文件夾這些異常,你必須手動走在目錄樹」

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

您可以檢查How to recursively search directories by using Visual C#

的目錄對象的GetDirectoriesGetFiles方法都是你需要遞歸搜索匹配搜索字符串的文件。下面的方法用於執行遞歸:

void DirSearch(string sDir) 
{ 
    try 
    { 
     foreach (string d in Directory.GetDirectories(sDir)) 
     { 
     foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
     { 
      lstFilesFound.Items.Add(f); 
     } 
     DirSearch(d); 
     } 
    } 
    catch (System.Exception excpt) 
    { 
     Console.WriteLine(excpt.Message); 
    } 
} 
+0

這些文件對我來說並不重要,所以我會刪除最內層的循環,並使用像'Directory.GetDirecotries(d,「GameData」);'而不是? – sh3rifme

+0

@ sh3rifme: - 你可以試試看! –

+0

因此,當我在桌面上使用它時會起作用,但如果我嘗試在C上使用,則失敗:/是否有原因? – sh3rifme

相關問題