Search code examples
c#winui-3storagefilestoragefolder

How to remove empty folders from TreeView?


i have a treeview which i load All Files and Folder from a Path. i used Microsoft Docs/Example

my codes is working fine, however there is one issue! i can see that there is Empty folders added into treeview. i want to remove them/skip them. i dont know how to do this. this is a little hard to me.

private async void FillTreeNode(TreeViewNode node)
{
    // Get the contents of the folder represented by the current tree node.
    // Add each item as a new child node of the node that's being expanded.

    // Only process the node if it's a folder and has unrealized children.
    StorageFolder folder = null;

    if (node.Content is StorageFolder && node.HasUnrealizedChildren == true)
    {
        folder = node.Content as StorageFolder;
    }
    else
    {
        // The node isn't a folder, or it's already been filled.
        return;
    }

    IReadOnlyList<IStorageItem> itemsList = await folder.GetItemsAsync();

    if (itemsList.Count == 0)
    {
        // The item is a folder, but it's empty. Leave HasUnrealizedChildren = true so
        // that the chevron appears, but don't try to process children that aren't there.
        return;
    }

    foreach (var item in itemsList)
    {
        var newNode = new TreeViewNode();
        newNode.Content = item;

        if (item is StorageFolder)
        {
            // If the item is a folder, set HasUnrealizedChildren to true.
            // This makes the collapsed chevron show up.
            newNode.HasUnrealizedChildren = true;
        }
        else
        {
            if (item is StorageFile storageFile)
            {
                if (!AllowedExtensions.Contains(storageFile.FileType))
                {
                    return;
                }
            }
            // Item is StorageFile. No processing needed for this scenario.
        }

        node.Children.Add(newNode);
    }

    // Children were just added to this node, so set HasUnrealizedChildren to false.
    node.HasUnrealizedChildren = false;
}

usage:

StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(rootPath);
TreeViewNode itemNode = new TreeViewNode();
itemNode.Content = storageFolder;
itemNode.IsExpanded = true;
itemNode.HasUnrealizedChildren = true;
sampleTreeView.RootNodes.Add(itemNode);
FillTreeNode(itemNode);

Solution

  • this code can fix 2 issues, first skip empty folder, second fix an issue where all items does not added.

    foreach (var item in itemsList)
    {
        var newNode = new TreeViewNode();
        newNode.Content = item;
    
        if (item is StorageFolder storageFolder)
        {
            var files = Directory.EnumerateFiles(storageFolder.Path, "*.*", SearchOption.AllDirectories)
                .Where(s => AllowedExtensions.Contains(Path.GetExtension(s)));
            if (files.Count() == 0)
            {
                continue;
            }
    
            // If the item is a folder, set HasUnrealizedChildren to true.
            // This makes the collapsed chevron show up.
            newNode.HasUnrealizedChildren = true;
        }
        else
        {
            if (item is StorageFile storageFile)
            {
                if (!AllowedExtensions.Contains(storageFile.FileType))
                {
                    continue;
                }
            }
            // Item is StorageFile. No processing needed for this scenario.
        }
    
        node.Children.Add(newNode);
    }