2011-09-24 103 views
0

我使用這個代碼:如何在枚舉目錄時訪問系統文件夾?

DirectoryInfo dir = new DirectoryInfo("D:\\"); 
foreach (FileInfo file in dir.GetFiles("*.*",SearchOption.AllDirectories)) 
{ 
    MessageBox.Show(file.FullName); 
} 

我得到這個錯誤:

UnauthorizedAccessException was unhandled

Access to the path 'D:\System Volume Information\' is denied.

如何我可能會解決這個問題?

+3

使用具有權限的帳戶運行程序? –

+0

我實際上想要解決這個問題... –

+1

我應該在代碼中添加什麼,以便程序可以忽略**系統文件夾Windows?**搜索 –

回答

1

.NET中沒有辦法覆蓋您正在運行此代碼的用戶的特權。

真的只有一個選項。確保只有管理員運行此代碼,或者在管理員帳戶下運行它。 這是可取的,你要麼把「嘗試catch」塊和處理這個異常或 運行代碼之前您檢查該用戶是管理員:

WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); 
WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity); 
if (currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator)) 
{ 
DirectoryInfo dir = new DirectoryInfo("D:\\"); 
foreach (FileInfo file in dir.GetFiles("*.*",SearchOption.AllDirectories)) 
{ 
MessageBox.Show(file.FullName); 
} 
} 
+0

此外,您可以*要求*應用程序在清單中以管理員身份運行。請參閱http://stackoverflow.com/questions/5276674/how-to-force-a-wpf-application-to-run-in-administrator-mode/5276770#5276770 – vcsjones

+0

此代碼不適合我... Err還在那裏! –

+0

在哪裏放** try-catch語句** 嘗試catch語句不應該打破我的循環 –

0

嘗試調用此方法將多一個try catch塊在呼叫之前 - 這將意味着頂級文件夾缺少所需的授權:

 static void RecursiveGetFiles(string path) 
    { 
     DirectoryInfo dir = new DirectoryInfo(path); 
     try 
     { 

      foreach (FileInfo file in dir.GetFiles()) 
      { 
       MessageBox.Show(file.FullName); 
      } 
     } 
     catch (UnauthorizedAccessException) 
     { 

      Console.WriteLine("Access denied to folder: " + path); 
     } 

     foreach (DirectoryInfo lowerDir in dir.GetDirectories()) 
     { 
      try 
      { 
       RecursiveGetFiles(lowerDir.FullName); 

      } 
      catch (UnauthorizedAccessException) 
      { 

       MessageBox.Show("Access denied to folder: " + path); 
      } 
     } 
    } 

} 
+0

問題在這裏 'FileInfo [] files = dir.GetFiles(「*。*」, '' –

+0

當我們調用'dir.GetFiles(「*。*」,SearchOption.AllDirectories);' 程序啓動文件搜索,包括「系統卷信息」文件夾 ,生成異常 –

+0

你可以嘗試以上.. 。除非用戶需要特權,否則這不會在目錄中進行搜索。我已經運行它反對我的C驅動器,它似乎工作。 –

0

您可以手動搜索忽略系統目錄的文件樹。

// Create a stack of the directories to be processed. 
Stack<DirectoryInfo> dirstack = new Stack<DirectoryInfo>(); 
// Add your initial directory to the stack. 
dirstack.Push(new DirectoryInfo(@"D:\"); 

// While there are directories on the stack to be processed... 
while (dirstack.Count > 0) 
{ 
    // Set the current directory and remove it from the stack. 
    DirectoryInfo current = dirstack.Pop(); 

    // Get all the directories in the current directory. 
    foreach (DirectoryInfo d in current.GetDirectories()) 
    { 
     // Only add a directory to the stack if it is not a system directory. 
     if ((d.Attributes & FileAttributes.System) != FileAttributes.System) 
     { 
      dirstack.Push(d); 
     } 
    } 

    // Get all the files in the current directory. 
    foreach (FileInfo f in current.GetFiles()) 
    { 
     // Do whatever you want with the files here. 
    } 
}