是否有可能以编程方式选择其中一个子节点

我想选择我的树视图的一个子节点以编程方式选择。 我的树视图在运行时如下

Root |->A.txt(I would like to select this node after doing some iteration in my application) |->Child(Even if i select this node and do some operations i would like to select the above one only like that if n number of child nodes exists i would like to select the node that was with .txt extension) 

我写了下面的代码它工作正常,但有一点我无法做到这一点可以任何一个帮助

这是我的代码

  if (tvwACH.Nodes.Count != 0) { // tvwACH.ExpandAll(); TreeNode tn; tn = tvwACH.Nodes[0]; tvwACH.ExpandAll(); if (tn.Nodes.Count != 0) { tn = tn.Nodes[0]; } if (tn.Tag.ToString() == "3") { if (tvwACH.SelectedNode.Parent != null) { tn.Parent.Expand(); tvwACH.SelectedNode = tn; } } } 

我的最终树视图如下

  Root |->Some.txt |->Child |->Sub Child |->Child (for subchild) // After this i will not have any nodes so my code works up to Sub Child but if i added some thing after clicking Child after subchild i am unable to select the node i meeded as it has no nodes can any one help me out please 

我得到了答案

  TreeNode TvNode = tvwACH.SelectedNode.Parent; while (TvNode != null) { if (TvNode.Text.EndsWith(".txt", true, System.Globalization.CultureInfo.InvariantCulture)) { tvwACH.SelectedNode = TvNode; TvNode = null; } else TvNode = TvNode.Parent; }