public static TreeNode MakeTreeFromPaths(List<string> paths, string rootNodeName = "E:\\", char separator = '\\')
{
var rootNode = new TreeNode(rootNodeName);
foreach (var path in paths.Where(x => !string.IsNullOrEmpty(x.Trim())))
{
var currentNode = rootNode;
var pathItems = path.Split(separator);
foreach (var item in pathItems)
{
var tmp = currentNode.Nodes.Cast<TreeNode>().Where(x => x.Text.Equals(item));
currentNode = tmp.Count() > 0 ? tmp.Single() : currentNode.Nodes.Add(item);
}
}
return rootNode;
}
This function can populate a treeview with a given list of file paths. But there is a problem, It will create a root node named rootNodeName and then will add other child nodes. How can I avoid given rootNodeName? I do not need any extra root node which is creating with this function.