忽略文件夹/文件时Directory.GetFiles()被拒绝访问 [英] Ignore folders/files when Directory.GetFiles() is denied access

查看:311
本文介绍了忽略文件夹/文件时Directory.GetFiles()被拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示所选目录中的所有文件的列表(也可以选择任何子目录)。我遇到的问题是,当的GetFiles()方法遇到它无法访问,它抛出一个异常文件夹并且进程将停止。

如何忽略此异常(并忽略受保护的文件夹/文件),并继续添加访问的文件到列表中?

 尝试
{
    如果(cbSubFolders.Checked == FALSE)
    {
        字符串[] =文件Directory.GetFiles(folderBrowserDialog1.SelectedPath);
        的foreach(在文件中字符串文件名)
            ProcessFile(文件名);
    }
    其他
    {
        字符串[] =文件Directory.GetFiles(folderBrowserDialog1.SelectedPath,SearchOption.AllDirectories*。*);
        的foreach(在文件中字符串文件名)
            ProcessFile(文件名);
    }
    lblNumberOfFilesDisplay.Enabled = TRUE;
}
赶上(UnauthorizedAccessException){}
最后{}


解决方案

您必须手动完成递归;不要使用AllDirectories - 一次看一个文件夹,然后尝试从子迪尔斯获得的文件。未经检验的,但类似下面(注意使用委托,而不是建立一个数组):

 使用系统;
使用System.IO;
静态类节目
{
    静态无效的主要()
    {
        字符串路径=; // 去做
        ApplyAllFiles(路径,ProcessFile);
    }
    静态无效ProcessFile(字符串路径){/ * ... * /}
    静态无效ApplyAllFiles(字符串的文件夹,动作<串GT; fileAction)
    {
        的foreach(在Directory.GetFiles字符串的文件(文件夹))
        {
            fileAction(文件);
        }
        的foreach(在Directory.GetDirectories串子目录(文件夹))
        {
            尝试
            {
                ApplyAllFiles(子目录,fileAction);
            }
            抓住
            {
                //燕子,日志,无论
            }
        }
    }
}

I am trying to display a list of all files found in the selected directory (and optionally any subdirectories). The problem I am having is that when the GetFiles() method comes across a folder that it cannot access, it throws an exception and the process stops.

How do I ignore this exception (and ignore the protected folder/file) and continue adding accessible files to the list?

try
{
    if (cbSubFolders.Checked == false)
    {
        string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
        foreach (string fileName in files)
            ProcessFile(fileName);
    }
    else
    {
        string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
        foreach (string fileName in files)
            ProcessFile(fileName);
    }
    lblNumberOfFilesDisplay.Enabled = true;
}
catch (UnauthorizedAccessException) { }
finally {}

解决方案

You will have to do the recursion manually; don't use AllDirectories - look one folder at a time, then try getting the files from sub-dirs. Untested, but something like below (note uses a delegate rather than building an array):

using System;
using System.IO;
static class Program
{
    static void Main()
    {
        string path = ""; // TODO
        ApplyAllFiles(path, ProcessFile);
    }
    static void ProcessFile(string path) {/* ... */}
    static void ApplyAllFiles(string folder, Action<string> fileAction)
    {
        foreach (string file in Directory.GetFiles(folder))
        {
            fileAction(file);
        }
        foreach (string subDir in Directory.GetDirectories(folder))
        {
            try
            {
                ApplyAllFiles(subDir, fileAction);
            }
            catch
            {
                // swallow, log, whatever
            }
        }
    }
}

这篇关于忽略文件夹/文件时Directory.GetFiles()被拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆