如果子文件夹和它们本身不包含任何文件,如何删除没有任何文件的子文件夹然后删除父文件夹

How to remove sub folders without any files then parent folders if their sub folders and themselves do not contain any files

我稍微修改了 codes 以仅使用 .PDF 文件填充我的 treeView1。它运行良好,除了留下许多空文件夹。是否有删除这些文件夹的方法?

  private void Begin()
    {
        treeView1.Nodes.Clear();
        if (Directory.Exists(FilePath))
            LoadDirectory(FilePath);
    }

    public void LoadDirectory(string Dir)
    {
        DirectoryInfo di = new DirectoryInfo(Dir);
        TreeNode tds = treeView1.Nodes.Add(di.Name);
        tds.Tag = di.FullName;
        tds.StateImageIndex = 0;
        LoadFiles(Dir, tds);
        LoadSubDirectories(Dir, tds);
    }

    private void LoadSubDirectories(string dir, TreeNode td)
    {
        // Get all subdirectories  
        string[] subdirectoryEntries = Directory.GetDirectories(dir);
        // Loop through them to see if they have any other subdirectories  
        foreach (string subdirectory in subdirectoryEntries)
        {
            DirectoryInfo di = new DirectoryInfo(subdirectory);
            TreeNode tds = td.Nodes.Add(di.Name);
            tds.StateImageIndex = 0;
            tds.Tag = di.FullName;
            LoadFiles(subdirectory, tds);
            LoadSubDirectories(subdirectory, tds);
        }
    }


    private void LoadFiles(string dir, TreeNode td)
    {
        string[] Files = Directory.GetFiles(dir, "*.PDF");
        // Loop through them to see files
        foreach (string file in Files)
        {
            FileInfo fi = new FileInfo(file);
            TreeNode tds = td.Nodes.Add(fi.Name);
            tds.Tag = fi.FullName;
            tds.StateImageIndex = 1;
        }
    }

我假设您在 TreeView 控件中留下了许多空文件夹,并且没有尝试清理文件系统。我这样做的方法是编写一个执行此清理的递归方法。递归允许我们遍历整个树并首先在最低级别采取行动,然后返回到根节点以确保 100% 的覆盖率,并尽可能少地进行额外处理。

这是实现此类解决方案的一种方法:

 private void CleanupEmptyNodes(TreeView thisTreeView)
    {
        List<TreeNode> emptyChildren = new List<TreeNode>();
        foreach (TreeNode topLevelNode in thisTreeView.Nodes)
        {
            var isEmpty = CleanupEmptyNodesRecursive(topLevelNode);
            if (isEmpty)
            {
                emptyChildren.Add(topLevelNode);
            }
        }
        foreach (TreeNode emptyChild in emptyChildren)
        {
            emptyChild.Remove();
        }
    }
    private bool CleanupEmptyNodesRecursive(TreeNode thisTreeNode)
    {
        List<TreeNode> emptyChildren = new List<TreeNode>();

        foreach(TreeNode node in thisTreeNode.Nodes)
        {
            var isEmpty = CleanupEmptyNodesRecursive(node);
            if (isEmpty)
            {
                emptyChildren.Add(node);
            }
        }

        //you can't delete the nodes from within the foreach
        foreach(TreeNode emptyChild in emptyChildren)
        {
            emptyChild.Remove();
        }
        
        //were all the nodes removed?
        if (thisTreeNode.Nodes.Count == 0)
        {
            if(/*perform test here to tell whether this node IS the file or not*/)
            {
                //is a file, so do not delete
                return false;
            }
            else
            {
                //this is a folder, and has no contents. Delete it!
                return true;
            }
        }
        else
        {
            //has files within it so do not delete
            return false;
        }
    }

那么你可以这样调用它:

CleanupEmptyNodes(treeView1);

就个人而言,我希望它成为一种扩展方法,这样我就可以 treeView1.CleanupEmptyNodes(); 但对每个人来说都是如此。