【问题标题】:How to get access to system folders when enumerating directories?枚举目录时如何访问系统文件夹?
【发布时间】:2011-11-24 08:43:52
【问题描述】:

我正在使用此代码:

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

我收到此错误:

UnauthorizedAccessException 未处理

对路径“D:\System Volume Information\”的访问被拒绝。

我该如何解决这个问题?

【问题讨论】:

  • 用有权限的账号运行程序?
  • 我真的想解决这个问题...
  • 我应该在我的代码中添加什么以便程序可以忽略 Windows 系统文件夹? 进行搜索
  • 这不是你问的,哈吉。
  • 系统卷信息存储还原点数据。即使使用管理员帐户也无法访问。您需要使用 GetDirectories() 并跳过您无法访问的目录。避免设置 FileAttributes.System 和 Hidden 的目录。

标签: c# .net unauthorizedaccessexcepti


【解决方案1】:

.NET 中无法覆盖运行此代码的用户的权限。

实际上只有 1 个选项。确保只有管理员运行此代码,或者您在管理员帐户下运行它。 建议您要么放置“try 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);
}
}

【讨论】:

  • 此外,您可以要求在清单中以管理员身份运行应用程序。见stackoverflow.com/questions/5276674/…
  • 放置 try-catch 语句的位置。 try catch 语句不应破坏我的循环
【解决方案2】:

尝试调用此方法,在调用之前再放置一个 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);
            }
        }
    }

}

【讨论】:

  • 问题在FileInfo[] files = dir.GetFiles("*.*",SearchOption.AllDirectories);
  • 当我们调用dir.GetFiles("*.*",SearchOption.AllDirectories);程序开始文件搜索,包括“系统卷信息”文件夹,产生异常
  • 你能在上面试试吗...这不会在目录中搜索,除非用户具有所需的权限。我已经在我的 C 盘上运行了它,它似乎可以工作。
  • 非常感谢...这是我的要求 :-)
  • 如果这对您有用,请将我的答案标记为正确,谢谢
【解决方案3】:

您可以手动搜索文件树而忽略系统目录。

// 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.
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多