2011-09-24 54 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. 
    } 
}