C# 如何获取选定节点的TreeView子节点以及上述节点?

C# 如何获取选定节点的TreeView子节点以及上述节点?,c#,.net,winforms,treeview,C#,.net,Winforms,Treeview,例如,如果我有根节点: private void treeView1_MouseClick(object sender, MouseEventArgs e) { // Get the node that was clicked TreeNode selectedNode = treeView1.HitTest(e.Location).Node; if (selectedNode != null) { } } Tree F

例如,如果我有根节点:

private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
    // Get the node that was clicked
    TreeNode selectedNode = treeView1.HitTest(e.Location).Node;

    if (selectedNode != null)
    {

    }
}
Tree                Full Path
==============================
1                   1
|__ 11              1\11
    |__ 111         1\11\111
    |__ 112         1\11\112
单击它时,我会看到以下节点:

World
例如,如果单击蓝色,我将在蓝色下看到更多节点

World
|____ Blue
|____ Green
|____ Red
|____ Black
|____ yellow
现在,如果我点击蓝色,我将得到选定的节点名称蓝色<代码>所选节点。名称将为蓝色

如果我点击Day将进入
selectedNode.Name
Day 但我想做的是,如果我在第二天点击
selectedNode
be 蓝色\天还是蓝色天

如果在Day下有另一个节点名1,我在selectedNode.name中单击1,我想查看BlueDay1或我更喜欢Blue\Day\1

我想要这个
\\
,这样我就可以用它作为目录名了。 问题是我正在使用
selectedNode.Name
作为目录来获取文件:

World
|____ Blue
|    |____ Day
|    |____ Night
|____ Green
|____ Red
|____ Black
|____ yellow
List ff=new List();
private void TreeView1u鼠标单击(对象发送器,鼠标目标e)
{
TreeNode selectedNode=treeView1.HitTest(e.Location).Node;
如果(selectedNode!=null)
{
字符串tt=mainPath+“\\”+selectedNode.Text;
ff=直接搜索(tt);
timer1.Enabled=true;
}
}
如果我点击蓝色,那么它会得到蓝色下的所有文件,包括子目录。但是,如果我单击1,并且1中有文件,那么它将不会获取任何文件,因为我需要完整的路径名Blue\Day\1才能从1获取文件

这就是我获取文件的方式

List<string> ff = new List<string>();
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
    TreeNode selectedNode = treeView1.HitTest(e.Location).Node;

    if (selectedNode != null)
    {
        string tt = mainPath + "\\" + selectedNode.Text;
        ff = DirSearch(tt);
        timer1.Enabled = true;
    }
}
静态列表目录搜索(字符串sDir)
{
列表文件=新列表();
尝试
{
foreach(Directory.GetDirectories(sDir)中的字符串d)
{
foreach(Directory.GetFiles(d)中的字符串f)
{
添加(f);
}
DirSearch(d);
}
}
捕获(系统异常excpt)
{
控制台写入线(除消息外);
}
归还文件;
}

我的想法是,如果我想得到蓝色下的所有文件,我点击蓝色,但如果我只想得到1中的文件,当我点击1时,它不起作用,因为1不是完整的路径

每个
TreeNode
都有一个从根树节点到当前树节点的路径。例如,您可以看到树的所有节点的完整路径,如下图所示:

static List<string> DirSearch(string sDir)
{
    List<string> files = new List<string>();
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d))
            {
                files.Add(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
    return files;
}
TreeView
有一个字符,用于获取或设置树节点路径使用的分隔符字符串,默认情况下为
\

如果希望对路径有更多的控制,可以使用或扩展方法

在下面提供的示例中,我创建了不带根节点的路径:

private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
    // Get the node that was clicked
    TreeNode selectedNode = treeView1.HitTest(e.Location).Node;

    if (selectedNode != null)
    {

    }
}
Tree                Full Path
==============================
1                   1
|__ 11              1\11
    |__ 111         1\11\111
    |__ 112         1\11\112

脱离主题,但使用
AfterSelect
事件而不是
MouseClick
。没有节点的属性AncestorsAndSelf。我应该从哪里得到它或者如何使用它?严重性代码说明项目文件行抑制状态错误CS1061“TreeNode”不包含“AncestorsAndSelf”的定义,并且找不到接受“TreeNode”类型的第一个参数的扩展方法“AncestorsAndSelf”(是否缺少using指令或程序集引用?)。我已在回答中发布了方法的链接。这是我写的一个扩展方法,我在代码中添加了TreeViewExtensions,并删除了静态代码,如果它是静态的,我会出错。但在选择此AncestorsAndSelf后的事件中仍然不存在:e.Node.AncestorsAndSelf()
TreeViewExtensions
应该是静态的,因为它包含扩展方法。我已使用s将链接代码更新为必需的
。只需将代码复制到
TreeViewExtensions.cs
中,并在项目中的任何位置使用它。在使其正常工作后,请阅读更多关于它的信息,它允许您在不继承的情况下向类添加一些方法。:)