简体   繁体   English

搜索所有子目录时如何忽略拒绝访问?

[英]How can i ignore access denied when searching to get all sub directories?

static IEnumerable<string> GetSubdirectoriesContainingOnlyFiles(string path)
{
    try
    {
        return from subdirectory in Directory.GetDirectories(path, "*", SearchOption.AllDirectories)
               where Directory.GetDirectories(subdirectory).Length == 0
               select subdirectory;
    }
    catch
    {

    }
}

In this case i'm searching in c:\\ So some directories are access denied. 在这种情况下,我正在c:\\中搜索,因此某些目录被拒绝访问。 I added try and catch but now this method dosent have a return. 我添加了try and catch,但是现在此方法返回了。

And how or should i handle at all it when it's getting to the catch ? 而且,当问题到来时,我应该怎么处理呢? I want in the end to get a List of all sub directories so i can get all sub directories names and the Length(the number of sub directories). 我想最后获得所有子目录的列表,以便我可以获取所有子目录的名称和Length(子目录的数量)。

UPDATE 更新

I tried this in the class constructor: 我在类构造函数中尝试过:

if (m_pars.SearchDir != null)
{
    ApplyAllFiles(m_pars.SearchDir,ProcessFile);
}

m_pars.SearchDir in this contain C:\\ m_pars.SearchDir在此包含C:\\

Then in ApplyAllFiles: 然后在ApplyAllFiles中:

static List<string> allsubdirs = new List<string>();
static void ProcessFile(string path) {/* ... */}
public 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);
            allsubdirs.Add(subDir);
        }
        catch
        {
            // swallow, log, whatever
        }
    }
}

But the List allsubdirs is empty. 但是List allsubdirs为空。

Your problem might be that you don't visit (add to the list) the current directory before recursively visiting its subdirectories. 您的问题可能是您递归访问子目录之前没有访问(添加到列表)当前目录。 So if you get an exception there, nothing will be added to the list. 因此,如果在那里出现异常,则不会将任何内容添加到列表中。

The following works for me. 以下对我有用。 (I've also made it a bit more generic by using callbacks and made the exception handling stricter.) (我还通过使用回调使它更加通用,并使异常处理更加严格。)

class DirectoryHelper
{
    public static void Test()
    {
        DirectoryHelper.EnumerateSubDirectories(@"c:\windows\system32");
    }

    public static List<string> EnumerateSubDirectories(string path)
    {
        // Depending on your use case, it might be 
        // unecessary to save these in memory 
        List<string> allSubdirs = new List<string>();
        EnumerateSubDirectories(path,
            filePath => Console.WriteLine("Visited file: " + filePath),
            dirPath => allSubdirs.Add(dirPath),
            noAccessPath => Console.WriteLine("No access: " + noAccessPath)
        );
        return allSubdirs;
    }

    private static void EnumerateSubDirectories(string root, Action<string> fileAction, Action<string> subdirAction, Action<string> noAccessAction)
    {
        foreach (string file in Directory.GetFiles(root))
        {
            fileAction(file);
        }

        foreach (string dir in Directory.GetDirectories(root))
        {
            try
            {
                subdirAction(dir);
                EnumerateSubDirectories(dir, fileAction, subdirAction, noAccessAction);
            }
            catch (UnauthorizedAccessException)
            {
                noAccessAction(dir);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果访问被拒绝,我该如何检查子目录和/或文件是否被拒绝? - How can i check also sub directories and/or files for access denied and to pas over if access denied? 如何在搜索时跳过被拒绝访问的目录或文件? - How can i pass over a directory or a file that is access denied when searching? 如何递归获取所有非系统目录? - How can I recursively get all non-system directories? 如何使用uribuilder获取目录下的所有子目录 - How to get all sub directories under a directory using uribuilder 获取文件时如何检查访问被拒绝? - How can i check for access denied when getting files? 获取所有仅包含文件的子目录 - Get all sub directories that only contain files 搜索包含特定字符串和删除字符串的目录和子目录中的所有文件 - Searching all files in directories and sub-directories that contain specific string and remove string 如何获取文件夹的目录,然后获取其目录的目录,依此类推……? - How can I get directories of a folder and then its directories' directory and so on…? 如何列出目录中的所有子目录 - how to list all sub directories in a directory 尝试访问用户信息时如何避免访问被拒绝的异常? - How can I avoid an access denied exception when trying to access User Information?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM