private void directoryMenu_Click(object sender, EventArgs e) { tree.ShowNodeToolTips = true; showDir (tree.Nodes, "c:\\", 2); } private void showDir (TreeNodeCollection target, string path, int levels) { try { DirectoryInfo info = new DirectoryInfo (path); TreeNode branch = new TreeNode(); branch.Text = info.Name; branch.ToolTipText = path; branch.Tag = path; target.Add (branch); if (levels > 1) { // using System.IO; DirectoryInfo[] dirs = info.GetDirectories(); foreach (DirectoryInfo subdir in dirs) showDir (branch.Nodes, subdir.FullName, levels - 1); } } catch (Exception e) { } } private void tree_BeforeExpand(object sender, TreeViewCancelEventArgs e) { if (e.Node.Nodes.Count == 0) { string path = (string)e.Node.Tag; try { DirectoryInfo info = new DirectoryInfo(path); DirectoryInfo[] dirs = info.GetDirectories(); foreach (DirectoryInfo subdir in dirs) showDir(e.Node.Nodes, subdir.FullName, 2); } catch (Exception ex) { } } else { foreach (TreeNode t in e.Node.Nodes) try { string path = (string) t.Tag; DirectoryInfo info = new DirectoryInfo(path); DirectoryInfo[] dirs = info.GetDirectories(); foreach (DirectoryInfo subdir in dirs) showDir(t.Nodes, subdir.FullName, 1); } catch (Exception ex) { } } }