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

Daniel van wolf
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. I added try and catch but now this method dosent have a return.

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).

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:\

Then in 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.

steinar

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);
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get `find` to ignore .svn directories?

How can I convert from uppercase to lowercase all directories and sub-directories in Ubuntu 16.04.2?

How can I ignore non dotfiles (all non hidden directories, in the root of my repository) with git ignore?

How can I get a listing of all directories with videos on them?

How can I get all of my GMail directories to appear in Thunderbird?

How can i check for access denied when getting files?

How can I let my Firebase Security Rules allow Authenticated Access to my Realtime Databas when I get "PERMISSION DENIED"

How can I add all files in sub-directories of assets in pubspec.yamal at once?

How can I set permission to all new sub directories on windows server created by java?

How can i add a recursive loop to delete a directory with all sub directories and files inside?

Why nautilus is not searching in sub directories when I search for uploading a document from chrome?

How can I get wget to preserve sub-directory structure while ignoring parent directories?

How to get Laravel to ignore filepaths when searching for a class?

jQuery: How can I get sub-menus to stay open when clicking sub-sub-menus

How do I ignore long lines in Sublime Text when searching

How to get image names from all sub directories?

How can I access inner directories in react

How can I run script for all directories?

How can i create sub directories within a directory?

How can I recursively find a directory by name and delete its contents (including all sub-directories and files) while keeping the directory itself?

How do I list all root directories and sub-directories in a MarkLogic database?

how do I search/detect all of the directories/sub-directories within a specified path?

How can I ignore a specific file in sub folder by using .gitignore?

How can I get breadcrumbs when searching for a file or folder in google drive api?

Windows 10: How can I fix "access denied" when using mkdir

Searching all files in directories and sub-directories that contain specific string and remove string

How can i delete all the files in all directories?

How can I restrict a bash find to only specific sub-sub-directories

How can I get `expect` to ignore Variables?