Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a photo viewer app using WPF.
The UI lists the drives & folders in the left pane using a treeview; on the right pane, it lists all the photos in the selected folder in the treeview.

While looping and adding items to the folder treeview, how do I filter out hidden & non-accessible folders?

Here is the current code:
C#
private void LoadDrives()
{
    DriveInfo[] allDrives = DriveInfo.GetDrives();

    foreach (DriveInfo drive in allDrives)
    {
        if (drive.DriveType == DriveType.Fixed || 
                  drive.DriveType == DriveType.Removable)
        {
            var drivetvItem = new TreeViewItem();
            drivetvItem.Header = drive.Name;
            drivetvItem.Tag = drive.Name;

            LoadParentFolders(drive.Name, drivetvItem);

            CanvasFolderBrowser.Items.Add(drivetvItem);
        }
    }
}

private TreeViewItem LoadParentFolders(string drive, TreeViewItem drivetvItem)
{
    DirectoryInfo dirInfo = new DirectoryInfo(drive);
    DirectoryInfo[] dirInfoList = dirInfo.GetDirectories();

    foreach (DirectoryInfo dir in dirInfoList)
    {
        var directorytvItem = new TreeViewItem();
        directorytvItem.Header = dir.Name;
        directorytvItem.Tag = dir.FullName;

        LoadChildFolders(dir.FullName, directorytvItem);

        drivetvItem.Items.Add(directorytvItem);
    }

    return drivetvItem;
}

private TreeViewItem LoadChildFolders
(string parentfolder, TreeViewItem directorytvItem)
{
    DirectoryInfo dirInfo = new DirectoryInfo(parentfolder);
    DirectoryInfo[] dirInfoList = dirInfo.GetDirectories();

    foreach (DirectoryInfo dir in dirInfoList)
    {
        var subdirectorytvItem = new TreeViewItem();
        subdirectorytvItem.Header = dir.Name;
        subdirectorytvItem.Tag = dir.FullName;

        LoadChildFolders(dir.FullName, subdirectorytvItem);

        directorytvItem.Items.Add(subdirectorytvItem);
    }

    return directorytvItem;
}


What I have tried:

I know fileinfo has the GetAttributes method. I can't find something similar to that for directoryinfo.
Posted
Updated 5-Oct-23 6:16am
v2
Comments
Graeme_Grant 5-Oct-23 7:50am    
Are you asking us how to write a filter or how to do the filtering based on your criteria?

The FileSystemInfos detail works for directories as well as files, see DirectoryInfo.GetFileSystemInfos Method (System.IO) | Microsoft Learn[^].
 
Share this answer
 
Both DirectoryInfo and FileInfo inherit from FileSystemInfo, which provides the Attributes property[^].

NB: Neither class has a GetAttributes method. Perhaps you're thinking of File.GetAttributes[^], which works for files or directories. But that works with paths represented as strings, not FileInfo / DirectoryInfo instances.

NB2: The attributes are only part of the story as to whether a file or directory is accessible. For example, on an NTFS drive, the DACL[^] might not allow the current user to access the object.
 
Share this answer
 
Comments
Christopher Fernandes 6-Oct-23 11:18am    
Yes about ACL too. It always throws an error when accessing a non authorised folder

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900