tree的我也做了一个,多谢各位啦! 我自己也在想想,打扰啦。有空帮忙看一眼,没时间就直接无视吧,刚刚开始学习。。。。
在选择一个node以后,为什么找不到它的正确路径?
[C#] 纯文本查看 复制代码
private void Form1_Load(object sender, EventArgs e)
{
TreeNode root = new TreeNode();
//根目录名称
root.Text = "Office";
//根目录路径
GetFiles("c:\\test", root);
treeView1.Nodes.Add(root);
}
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.GetFullPath(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);
}
|