Ignore UnauthorizedAccessException while getting Files and SubDirectories of a directory

97 Views Asked by At

I am using the code below to get the Files and the SubDirectories of a directory and then populate a TreeView control. I am getting an UnauthorizedAccessException exception. I tried to handle it using a try and catch but in vain...

void GetFilesAndSubDirs(DirectoryInfo root, TreeNodeCollection nodes)
{
    FileInfo[] files = null;
    DirectoryInfo[] subDirs = null;

    try
    {
        files = root.GetFiles("*.*");
        subDirs = root.GetDirectories();
    }
    catch (UnauthorizedAccessException e)
    {
        MessageBox.Show(e.Message);
    }

    catch (DirectoryNotFoundException e)
    {
        MessageBox.Show(e.Message);
    }

    TreeNode parent = FindNode(root.Name, nodes);

    if (files != null)
    {
        foreach (FileInfo fiInfo in files)
        {
            TreeNode fileNode = new TreeNode(fiInfo.Name);
            fileNode.ImageIndex = 1;
            fileNode.SelectedImageIndex = 1;
            parent.Nodes.Add(fileNode);
        }
    }

    if (subDirs != null)
    {
        foreach (DirectoryInfo dirInfo in subDirs)
        {
            TreeNode dirNode = new TreeNode(dirInfo.Name);
            dirNode.ImageIndex = 0;
            dirNode.SelectedImageIndex = 0;
            parent.Nodes.Add(dirNode);
            GetFilesAndSubDirs(dirInfo, parent.Nodes);
        }
    }
}

UPDATE #1

When I comment the line of the recursive call, it works just fine.

enter image description here enter image description here

2

There are 2 best solutions below

2
vsir On

Is this expected behavior or should you have rights to access this directory?

Have you tried running Visual Studio as an administrator? You as a user might have rights to view it but the application does not necessarily do.

5
riffnl On

Although the answer and comment are correct to some degree. They're not handling the issue at hand; you "try to catch but in vain" - of course it is in vain - you made it so.

At first you try to get the directory which is named root - you'll get the exception and still try to continue with that same "root" variable, which will be null or at least not set correctly.

When you receive an error message (and for some reason just relate that message to the user directly) you should stop the process. You have an exception (which is by all means a reason to stop processing - it's an unexpected error) - you could never assume the process after the exception is going to run as expected.

I suggest you (in this case) show the messagebox and "return" and don't go forward into the process.

Although it's not holy or sacred - I suggest you'd read up on "defensive programming (C#)"

EDIT #1

Alter the beginning of the method along the line of this:

void GetFilesAndSubDirs(DirectoryInfo root, TreeNodeCollection nodes)
{
    FileInfo[] files = null;
    DirectoryInfo[] subDirs = null;

    try
    {
        files = root.GetFiles("*.*");
        subDirs = root.GetDirectories();
    }
    catch (UnauthorizedAccessException e)
    {
        MessageBox.Show(e.Message);
        return; // unexpected behavior : notice to user and stop
    }

    catch (DirectoryNotFoundException e)
    {
        MessageBox.Show(e.Message);
        return; // unexpected behavior : notice to user and stop
    }