/// <summary>
/// 根据路径得到选择节点
/// </summary>
/// <param name="path">路径</param>
/// <param name="treeView">树</param>
/// <returns>节点</returns>
private TreeNode GetNodeByPath(string path, TreeView treeView)
{
TreeNode node = new TreeNode();
string[] pathLevel = path.Split('\\');
int i = 0;
foreach (TreeNode topNode in treeView.Nodes)
{
if (topNode.Text == pathLevel[i].ToString())
{
node = topNode;
i++;
break;
}
}
if (i < pathLevel.Length)
{
node = GetSubNode(node, pathLevel, i);
}
return node;
}
private TreeNode GetSubNode(TreeNode node, string[] pathLevel, int i)
{
TreeNode newNode = new TreeNode();
foreach (TreeNode subNode in node.Nodes)
{
if (subNode.Text == pathLevel[i].ToString())
{
newNode = subNode;
i++;
if (i == pathLevel.Length)
{
break;
}
if (i < pathLevel.Length)
{
newNode = GetSubNode(newNode, pathLevel, i);
}
}
}
return newNode;
}
来源:https://www.cnblogs.com/neil-raul/archive/2007/07/04/805202.html