打开任意一个文件夹,生成这个文件夹的Tree view树形图

打开任意一个文件夹,生成这个文件夹的Tree view树形图
打开任意一个文件夹,生成这个文件夹的Tree view树形图
打开任意一个文件夹,生成这个文件夹的Tree view树形图

以下是一段简单的 Python 代码,可以生成指定文件夹的树形图:

import os

def print_directory_contents(path):
    for child in os.listdir(path):
        child_path = os.path.join(path, child)
        if os.path.isdir(child_path):
            print(f"- {child}/")
            print_directory_contents(child_path)
        else:
            print(f"- {child}")

print_directory_contents("/path/to/folder")

上面的代码会打印出文件夹内的所有文件及子文件夹,以树形结构的方式展示出来。

这两个是尝试的

  public static string Ab2RePath(string targetPath, string basePath)//绝对路径相对路径转化
        {
            const string directorySeparatorChar = "\\";
            Uri pathUri = new Uri(targetPath);

            if (!basePath.EndsWith(directorySeparatorChar))
            {
                basePath += directorySeparatorChar;
            }
            Uri folderUri = new Uri(basePath);
            return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace("/", directorySeparatorChar));
        }



        public static void addNodes(DirectoryInfo TheFolder, TreeNode node)//递归报错!
        {
            TreeNode subnode = new TreeNode();
            DirectoryInfo[] dirInfo = TheFolder.GetDirectories();//主文件夹的子文件夹
            FileInfo[] fileInfo = TheFolder.GetFiles("*.xml");//主文件夹的文件
            if (dirInfo.Length > 0)
            {
                foreach (DirectoryInfo tempdirInfo in dirInfo)
                {
                    subnode.Text = tempdirInfo.Name;
                    node.Nodes.Add(subnode);
                    if (tempdirInfo.GetDirectories() != null)
                    {
                        addNodes(tempdirInfo, subnode);
                    }
                }
            }
        }