树的实现实例
private void btnLoad_Click(object sender, EventArgs e) { string rootPath = txtPath.Text; TreeNode root = new TreeNode(); //创建树节点 root.Text = Path.GetFileName(rootPath); //截取路径中的文件夹名部分 root.Tag = rootPath; //为了将来加载子目录信息,保存目录的完整路径 tvDir.Nodes.Add(root); //为树控件添加节点 this.LoadSubNode(root); //开始递归调用 }
//递归调用的方法
private void LoadSubNode(TreeNode parentNode) { string path = parentNode.Tag.ToString(); //获取选中节点的完整路径 string[] dirs = Directory.GetDirectories(path); //获取保存子目录的字符串数组
//循环添加子节点到父节点中 foreach (string subPath in dirs) { TreeNode node = new TreeNode(); node.Text = Path.GetFileName(subPath); node.Tag = subPath; parentNode.Nodes.Add(node); this.LoadSubNode(node); //递归调用 } }
本站技术原创栏目文章均为中睿原创或编译,转载请注明:文章来自中睿,本站保留追究责任的权利。