计算机目录的树状视图c#

本文关键字:视图 计算机 | 更新日期: 2023-09-27 17:51:01

我想访问使用文件夹浏览器的计算机目录,然后所选路径的值应该用来显示树形视图。谁能告诉我在按钮单击中调用LISTDIRECTORY方法时使用什么参数?此外,我不知道我做错了什么,请帮助

     private void button2_Click(object sender, EventArgs e)
        {
            tView.Nodes.Clear();
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                string[] folders = Directory.GetDirectories(folderBrowser.SelectedPath);
                foreach (string path in folders)
                {
                    ListDirectory(tView.Nodes,file.Name);
                }
            }
        }
        private static void ListDirectory(TreeView treeView,string  path)
        {

            var stack = new Stack<TreeNode>();
            var rootDirectory = new DirectoryInfo(path);
            var node = new TreeNode(rootDirectory.Name) { Tag = rootDirectory };
            stack.Push(node);
            while (stack.Count > 0)
            {
                var currentNode = stack.Pop();
                var directoryInfo = (DirectoryInfo)currentNode.Tag;
                foreach (var directory in directoryInfo.GetDirectories())
                {
                    var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
                    currentNode.Nodes.Add(childDirectoryNode);
                    stack.Push(childDirectoryNode);
                }
                foreach (var file in directoryInfo.GetFiles())
                    currentNode.Nodes.Add(new TreeNode(file.Name));
            }
            tView.Nodes.Add(node);
        }
    }
}

计算机目录的树状视图c#

使用以下代码代替您的代码:

    private void button2_Click(object sender, EventArgs e)
    { 
      if (folderBrowser.ShowDialog() == DialogResult.OK)
        {
            string strSearchPath=folderBrowserDialog1.SelectedPath;
            GetTree(strSearchPath);
        }
    } 
   void GetTree(string strSearchPath)
        {
            tView.Nodes.Clear();
            SetNode(tView, strSearchPath);
            tView.TopNode.Expand();
        }
        void SetNode(TreeView treeName, string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            TreeNode node = new TreeNode(dirInfo.Name);
            node.Tag = dirInfo;
            GetFolders(dirInfo, node);
            GetFiles(dirInfo, node);
            treeName.Nodes.Add(node);
        }
        void GetFolders(DirectoryInfo d, TreeNode node)
        {    
            try
            {
                DirectoryInfo[] dInfo = d.GetDirectories();    
                if (dInfo.Length > 0)
                {
                    TreeNode treeNode = new TreeNode();
                    foreach (DirectoryInfo driSub in dInfo)
                    {
                        treeNode = node.Nodes.Add(driSub.Name, driSub.Name, 0, 0);
                        GetFiles(driSub, treeNode);
                        GetFolders(driSub, treeNode);
                    }
                }
            }
            catch (Exception ex) { }
        }
        void GetFiles(DirectoryInfo d, TreeNode node)
        {
           //if you want to search .doc or docx file then:
           // var files = d.GetFiles("*.doc*");
            var files = d.GetFiles("*.*");
            FileInfo[] subfileInfo = files.ToArray<FileInfo>();
            if (subfileInfo.Length > 0)
            {
                for (int j = 0; j < subfileInfo.Length; j++)
                {
                   //Checking for Hiddend files
                    bool isHidden = ((File.GetAttributes(subfileInfo[j].FullName) & FileAttributes.Hidden) == FileAttributes.Hidden);
                    if (!isHidden)
                    {                       
                            TreeNode treeNode = new TreeNode();
                            string path = subfileInfo[j].FullName;
                            string name = subfileInfo[j].Name;
                            treeNode = node.Nodes.Add(path, name);                 
                    }
                }
            }
        }

希望这对你有帮助

或者你必须传递一个TreeView而不是TreeView. nodes。或者你可以将参数类型改为Nodes,这样你就可以传递节点而不是整个树视图。

               ListDirectory(**tView.Nodes**,file.Name);
            }
        }
    }
    private static void ListDirectory(**TreeView treeView**,string  path)
    {

HTH约翰。