初学者求助:如何得到node的路径
自己做了一个treeview, 把c盘下test的所有文件放到treeview上。然后展开点击其中一个node,把这个node下的folder复制到listbox中,点击node以后不能得到node的正确路径,要怎么才能得到正确路径哪?就是C:\test\releases.....
下面是code和错误,点击其中一个node,得到的路径为什么是工程所在的路径而不是文件夹的路径哪? 多谢解答!!!
[local]1[/local]
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using using System.Diagnostics; namespace tREEVIEW { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { string selectnodepath = e.Node.FullPath.ToString(); } private void Form1_Load(object sender, EventArgs e) { TreeNode root = new TreeNode(); //根目录名称 //根目录路径 GetFiles("c:\\test", root); treeView1.Nodes.Add(root); root.Tag = root.FullPath; } public void GetFiles(string filePath, TreeNode node) { DirectoryInfo folder = new DirectoryInfo(filePath); node.Text = folder.Name; node.Tag = folder.FullName; FileInfo[] chldFiles = folder.GetFiles("*.*"); foreach (FileInfo chlFile in chldFiles) { TreeNode chldNode = new TreeNode(); chldNode.Text = chlFile.Name; chldNode.Tag = chlFile.FullName; node.Nodes.Add(chldNode); } DirectoryInfo[] chldFolders = folder.GetDirectories(); foreach (DirectoryInfo chldFolder in chldFolders) { TreeNode chldNode = new TreeNode(); chldNode.Text = folder.Name; chldNode.Tag = folder.FullName; node.Nodes.Add(chldNode); GetFiles(chldFolder.FullName, chldNode); } } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(treeView1.SelectedNode.FullPath.ToString()); string showsearch = Path.GetDirectoryName(treeView1.SelectedNode.FullPath.ToString()); DirectoryInfo test = new DirectoryInfo(showsearch); DirectoryInfo[] searchinfo = test.GetDirectories(); foreach (DirectoryInfo news in searchinfo) { if (news.Name.Contains(textBox1.Text)) { //treeView1.Nodes.Add(news.Name); listBox1.Items.Add(news.Name); } } } private void button2_Click(object sender, EventArgs e) { ProcessStartInfo proc = new ProcessStartInfo(); proc.UseShellExecute = true; proc.FileName = "C:\\test.bat"; proc.Verb = "runas"; try { Process.Start(proc); } catch { return; } Application.Exit(); // Quit itself if (treeView1.SelectedNode.IsSelected==true) { Process.Start(treeView1.SelectedNode.ToString()); } else { Process.Start(listBox1.SelectedItem.ToString()); } } private void listBox1_Doubleclick(object sender, EventArgs e) { foreach (DirectoryInfo f in new DirectoryInfo(listBox1.SelectedItem.ToString()).GetDirectories()) { MessageBox.Show(listBox1.SelectedItem.ToString()); listBox1.Items.Add(f.FullName); } } } }