C#中用Treeview控件加载整个硬盘

实现代码:

//加载节点 private void FillNode(TreeNode TN) { if (TN.Text.ToString() != "我的电脑") { EnumDirectories(TN); } else { EnumDrives(TN); } } // 加载盘符 private void EnumDrives(TreeNode ParentNode) { if (ParentNode.Nodes.Count == 0) { foreach (string drive in Directory.GetLogicalDrives()) { treeView_disk.SelectedNode = ParentNode; TreeNode TempNode = new TreeNode(); TempNode.Text = drive.Substring(0, drive.Length - 1); TempNode.Tag = drive; TempNode.ImageIndex = 3; TempNode.SelectedImageIndex = 3; treeView_disk.SelectedNode.Nodes.Add(TempNode); treeView_disk.SelectedNode.Nodes[treeView_disk.SelectedNode.Nodes.Count - 1].EnsureVisible(); FillNode(TempNode); } } } // 递归实现 加载目录 private void EnumDirectories(TreeNode ParentNode) { treeView_disk.SelectedNode = ParentNode; string DirectoryPath = ParentNode.Tag.ToString(); if (ParentNode.Nodes.Count == 0) { if (DirectoryPath.Substring(DirectoryPath.Length - 1) != @"/") DirectoryPath += @"/"; try { foreach (string directory in Directory.GetDirectories(DirectoryPath)) { TreeNode TempNode = new TreeNode(); TempNode.Text = directory.Substring(directory.LastIndexOf(@"/") + 1); TempNode.Tag = directory; TempNode.ImageIndex = 1; TempNode.SelectedImageIndex = 2; treeView_disk.SelectedNode.Nodes.Add(TempNode); treeView_disk.SelectedNode.Nodes[treeView_disk.SelectedNode.Nodes.Count - 1].EnsureVisible(); FillNode(TempNode); } } catch (Exception) { //异常发生 } } } private void treeView_disk_AfterSelect(object sender, TreeViewEventArgs e) { } //初始化 FillNode(treeView_disk.Nodes[0]);

你可能感兴趣的:(exception,String,object,C#,disk)