简体   繁体   中英

How to show all directory and file under drive

I'm trying to create a simple Window explorer by TreeView and ListView in C# and following this guide .

My code work very well when I choose a directory but it does not work when I choose drive like C:\\

I try to write a function for the case when a drive is chosen:

private void PopulateTreeViewWithDriver()
{
    string[] temp = 
    Directory.GetDirectories(fbd_Dialog.SelectedPath.ToString());
    DirectoryInfo[] temp1 = new DirectoryInfo[temp.Length];

    for (int i = 0; i < temp.Length; i++)
    {
        temp1[i] = new DirectoryInfo(temp[i]);
    }

    TreeNode[] array = new TreeNode[temp1.Length];

    for (int i = 0; i < temp1.Length; i++)
    {
        array[i] = new TreeNode(temp1[i].Name);
        array[i].Tag = temp1[i];
        GetDirectories(temp1[i].GetDirectories(), array[i]);
    }

    TreeNode rootNode;
    rootNode = new TreeNode(fbd_Dialog.SelectedPath.ToString(), array);
    tv_Folder.Nodes.Add(rootNode);
}

But I get this error:

"Access to the path 'C:\\Documents and Settings' is denied.'"

This picture shows the result when I choose a directory:

该图片是我选择目录后的结果

There are directories which you cannot show, because of access rights. When I was in a similar situation, I was simply handling the SecurityException and treating it as an indication that I cannot access the directory.

By the way, someone mentioned Admin rights. That is not the right way either, because there are some system-critical directories and files which even the Admin user cannot access.

On a more philosophical level, what would it mean if a common user started your application and then succeeded to read the directories for which it doesn't have permissions? Such tool would allow users to spy other users of the same computer, which would deny the whole purpose of OS-built-in file system security.

Bottom line - ignore all SecurityExceptions , skip over them and work with other directories for which you don't receive an exception back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM