0

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

imanebz
  • 79
  • 1
  • 11

2 Answers2

0

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.

vsir
  • 349
  • 2
  • 12
  • Can you access that directory with your current user? Does it require admin privileges? Michael Randall has given the same suggestions. If running VS as admin or using a user with sufficient rights does not help then I do not know what the cause may be :/. – vsir May 06 '20 at 23:26
  • Yes I know, I really don't know the cause :( – imanebz May 06 '20 at 23:30
0

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
    }
riffnl
  • 3,248
  • 1
  • 19
  • 32
  • When I comment the line of recursion it works perfectly for root though – imanebz May 06 '20 at 23:29
  • Sure it does - for root - so somewhere along the line you *don't* have access... you should program / be ready for that occurrence. I will add some code for revision for you. – riffnl May 06 '20 at 23:30
  • Even after this code sample: you could run into an unexpected exception you didn't notice before.. – riffnl May 06 '20 at 23:34
  • But how can I ignore that and just list the directories/files to which the application has acces? – imanebz May 06 '20 at 23:37
  • You can't.. you don't have access - which is the reason you got the exception in the first place.. You could however enforce administrator rights on your application - see this answer https://stackoverflow.com/questions/3598824/how-to-force-my-c-sharp-winforms-program-run-as-administrator-on-any-computer – riffnl May 06 '20 at 23:41