今天在看treeview控件的使用,在测试例程的时候出现了问题。主要代码如下:
1 private void LoadTree() 2 { 3 DirectoryInfo directory; 4 treeExplorer.Nodes.Clear(); 5 6 foreach (string drive in Environment.GetLogicalDrives())
7 { 8 try 9 { 10 directory = new DirectoryInfo(drive); 11 if (directory.Exists)
12 { 13 TreeNode node = new TreeNode(directory .FullName ); 14 treeExplorer.Nodes.Add(node); 15 GetSubDirectories(node);
16 } 17 } 18 catch 19 { 20 return; 21 } 22 } 23 } 24 25 private void GetSubDirectories(TreeNode Parent) 26 { 27 DirectoryInfo directory;
28 try 29 { 30 if (Parent.Nodes.Count == 0) 31 { 32 directory = new DirectoryInfo(Parent.FullPath); 33 DirectoryInfo[] directories = directory.GetDirectories();
34 foreach (DirectoryInfo dir in directories) 35 { 36 TreeNode newnode = new TreeNode(dir.FullName); 37 Parent.Nodes.Add(newnode); 38 } 39 } 40 41 foreach (TreeNode node in Parent.Nodes)
42 { 43 if (node.Nodes.Count == 0) 44 { 45 directory = new DirectoryInfo(node.FullPath); 46 foreach (DirectoryInfo dir in directory.GetDirectories()) 47 {
48 TreeNode newnode = new TreeNode(dir.Name); 49 node.Nodes.Add(newnode); 50 } 51 }
52 } 53 } 54 catch(Exception ex) 55 { 56 //MessageBox.Show(ex.ToString ()); 57 return; 58 } 59 }
问题出现在第45行的node.FullPath,截图如下:
调了两个多小时,还没调出个所以然,希望各位大侠可以指点指点...
你把
TreeNode node = new TreeNode(directory.FullName );
改成
TreeNode node = new TreeNode(directory.Name );
=====================================
把
TreeNode newnode = new TreeNode(dir.FullName);
改成
TreeNode newnode = new TreeNode(dir.Name);
你确定你的路径没错吗?
你说的路径指的是什么?
我的问题就是处在路径上,目录或路径都是通过调用系统方法得到的,我就是弄不明白为什么node.FullPath会是"C:\\\\C:\\***"(如图:“C:\\\\\C:\\360Rec”),正确的路径应该是"C:\\***"(如:C:\\360Rec)
@Waming:
第13行:TreeNode node = new TreeNode(string text);//text表示node的Text属性;
第32行: directory = new DirectoryInfo(Parent.FullPath);//Parent是节点,那么要去盘符,应该是Text属性,应修改为:directory = new DirectoryInfo(Parent.Text);
第45行:同第32行。应修改为:directory = new DirectoryInfo(node.Text);