I'm trying to populate a TreeList from a list of folder path, for example:
C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\AppPatch\MUI\040C
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409
and output I want like this
├───addins
├───AppPatch
│ └───MUI
│ └───040C
├───Microsoft.NET
│ └───Framework
│ └───v2.0.50727
│ └───MUI
│ └───0409
Notice: I am using Devexpress TreeList.
This is my code:
private void PopulateTreeList(TreeList treeList, IEnumerable<string> paths, char pathSeparator)
{
TreeListNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeListNode nodes = treeList.FindNode((node) => { return node[""].ToString() == subPath; });
if (nodes == null)
lastNode = treeList.AppendNode(new object[] { subPath }, lastNode);
else
{
if(subPathAgg== GetFullPath(nodes, "\\"))
lastNode = nodes;
else
lastNode = treeList.AppendNode(new object[] { subPath }, lastNode);
}
}
lastNode = null;
}
}
private string GetFullPath(TreeListNode node, string pathSeparator)
{
if (node == null) return "";
string result = "";
while (node != null)
{
result = node.GetDisplayText(0) + pathSeparator + result;
node = node.ParentNode;
}
return result;
}
The TreeList.FindNode performing search in all nodes. You can use TreeListNodes.FirstOrDefault method to make search only in child nodes of current node.
Here is example:
private void PopulateTreeList(TreeList treeList, IEnumerable<string> paths, char pathSeparator)
{
foreach (string path in paths)
{
TreeListNode parentNode = null;
foreach (string folder in path.Split(pathSeparator))
{
var nodes = parentNode?.Nodes ?? treeList.Nodes;
var node = nodes.FirstOrDefault(item => item[0].Equals(folder));
if (node == null)
node = treeList.AppendNode(new object[] { folder }, parentNode);
parentNode = node;
}
}
}
Here is the result:
You can run example by this code:
var paths = new[]
{
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\AppPatch\MUI\040C",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
var treeList = new TreeList() { Size = new Size(400, 250) };
var column = treeList.Columns.Add();
column.VisibleIndex = 0;
PopulateTreeList(treeList, paths, Path.DirectorySeparatorChar);
treeList.ExpandAll();
var form = new Form() { Size = new Size(500, 350) };
form.Controls.Add(treeList);
form.Show();
Related
I have TreeNode node.
something = treeview.Nodes[1].Nodes[4].Nodes[0];
TreeNode myNode = something;
And wish to know how pany parents it has and what indexes i need to use to find whole path out from this node.
I mean get "treeview.Nodes[1].Nodes[4].Nodes[0]" out from myNode
Something like this should work:
public IList<int> GetNodePathIndexes(TreeNode node)
{
List<int> indexes = new List<int>();
TreeNode currentNode = node;
while (currentNode != null)
{
TreeNode parentNode = currentNode.Parent;
if (parentNode != null)
indexes.Add(parentNode.Nodes.IndexOf(currentNode));
else
indexes.Add(currentNode.TreeView.Nodes.IndexOf(currentNode));
currentNode = parentNode;
}
indexes.Reverse();
return indexes;
}
You can then look at the result of this to get the indexes, and the count, to get the number of parents.
IList<int> path = GetNodePathIndexes(myNode);
StringBuilder fullPath = new StringBuilder("treeview");
foreach (int index in path)
{
fullPath.AppendFormat(".Nodes[{0}]", index);
}
Then fullPath.ToString() should return treeview.Nodes[1].Nodes[4].Nodes[0]
//...
string path = GetPath(treeView1.Nodes[0].Nodes[0].Nodes[1].Nodes[0]);
// now path is "treeView.Nodes[0].Nodes[0].Nodes[1].Nodes[0]"
//...
string GetPath(TreeNode node) {
int index;
Stack<string> stack = new Stack<string>();
while(node != null) {
if(node.Parent != null) {
index = node.Parent.Nodes.IndexOf(node);
stack.Push(string.Format("Nodes[{0}]", index));
}
else {
index = node.TreeView.Nodes.IndexOf(node);
stack.Push(string.Format("treeView.Nodes[{0}]", index));
}
node = node.Parent;
}
return string.Join(".", stack.ToArray());
}
I try to populate a TreeView at runtime hierarchically but something wrong is happening in my code.
Normally I should have many projects as the root nodes, followed by sub projects as sub nodes and so on.
Let's say projects 1, 9, 10, 62, and 65 are the roots or projects.
Project #1 contains sub projects 17, 24, 33, 34, 35, 61, 98, ...
Project #9 contains no sub projects.
Project #10 contains sub projects 2 and 104.
Project #62 contains sub projects 63, 64, 108, 109.
Project #65 again has other sub projects.
Problem: The code keeps adding root nodes to each other. So it considers the next root node as the child of the previous one.
Result: The code should create separated root nodes with sub projects (sub projects can also contain subs).
Code:
private void button2_Click(object sender, EventArgs e)
{
DbConnector db = new DbConnector();
string str = "";
List<string> lst = db.ReadProjectsTable();
lst.OrderBy(x => x.Count(y => y == '|'));
List<string> newLst = new List<string>();
foreach (var item in lst)
{
string output = "";
foreach (var item2 in item.Split('|', '|'))
{
output += item2 + '-';
}
output = output.Substring(output.IndexOf('-')+1, output.LastIndexOf('-')-2);
newLst.Add(output);
str += output + Environment.NewLine;
}
textBox2.Text = str;
tvProjects.PathSeparator = #"-";
PopulateTreeView(tvProjects, newLst, '-');
}
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
}
}
UPDATE:
Sample data of List<string> lst
|1|
|9| |10| |62| |65| |67| |78| |83| |86| |105| |116| |125| |10|2| |67|4|
|1|17| |1|24| |1|33| |1|34| |1|35| |1|61| |62|63| |62|64| |67|68|
|65|69| |65|70| |65|71| |65|72| |65|75|
You should reset the lastNode variable to null before entering the inner loop. And to avoid confusion and similar errors, better declare and initialize variables at the place where they are needed:
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
foreach (string path in paths)
{
string subPathAgg = string.Empty;
TreeNode lastNode = null;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
}
}
I need to expand my treeview based on a fullpath in c#
My tree view has 2 nodes which are collapsed and I want to expand Node A to number 3
so I have the fullpath of node A\1\2\3.
How can I step through and open each node based of the fullpath? Also the length of the fullpath may change, so i may need to open node be to level 6. So it needs to be done based on the fullpath. Any help would be great.
Node A
1
2
3
Node B
1
2
3
4 5 6
This is what I've tried:
TreeNode [] n= treeView1.Nodes.Find(search, true);
if (n.Length > 0)
found = true;
treeView1.Nodes[t].Collapse();
foreach (TreeNode p in n) {
string[] a = p.FullPath.Split('\\');
foreach (string b in a) {
treeView1.SelectedNode = treeView1.Nodes[b];
treeView1.SelectedNode.Expand();
I'm sorry for not commenting on above answer given by S3ddi9 which is CORRECT. I'm only adding something.
So the answer given by S3ddi9
...
string path = #"A\1\2\";
var path_list = path.Split('\\').ToList();
foreach (TreeNode node in treeView1.Nodes)
if (node.Text == path_list[0])
ExpandMyLitleBoys(node, path_list);
}
private void ExpandMyLitleBoys(TreeNode node, List<string> path)
{
path.RemoveAt(0);
node.Expand();
if (path.Count == 0)
return;
foreach (TreeNode mynode in node.Nodes)
if (mynode.Text == path[0])
{
ExpandMyLitleBoys(mynode, path); //recursive call
break; //this was missing in earlier answer
}
}
Does work, BUT you must add a BREAK (I marked it), because if the for loop doesn't finish, return; won't return you to your main function and it will throw you an exception because path[0] is null.
I hope this will be more simpler, for Treeviews the best approach is to use recursive methods
...
string path = #"A\1\2\";
var path_list = path.Split('\\').ToList();
foreach (TreeNode node in treeView1.Nodes)
if (node.Text == path_list[0])
ExpandMyLitleBoys(node, path_list);
}
private void ExpandMyLitleBoys(TreeNode node, List<string> path)
{
path.RemoveAt(0);
node.Expand();
if (path.Count == 0)
return;
foreach (TreeNode mynode in node.Nodes)
if (mynode.Text == path[0])
ExpandMyLitleBoys(mynode, path); //recursive call
}
this does the work perfectly
Cracked it!!
TreeNode[] n = treeView1.Nodes.Find(search, true);
if (n.Length > 0)
found = true;
treeView1.Nodes[t].Collapse();
foreach (TreeNode p in n)
{
i = 0;
string[] a = p.FullPath.Split('\\');
foreach (string b in a)
{
if (i == 0)
{
treeView1.SelectedNode = treeView1.Nodes[b];
current = treeView1.Nodes[b];
treeView1.SelectedNode.Expand();
i = 1;
}
else
{
treeView1.SelectedNode = current.Nodes[b];
current = current.Nodes[b];
treeView1.SelectedNode.Expand();
if (b.ToUpper().Contains(search))
{
treeView1.SelectedNode.BackColor = System.Drawing.Color.Red;
}
I wrote a little simpler routine that works great. no recursion at all...
This assumes your path is a full file path like... "C:\program files\myapp" and when you add your nodes you set the node Key equal to the folder name
string[] strFolders = strPath.Split('\'));
System.Windows.Forms.TreeNode CurrentNode = null;
System.Windows.Forms.TreeNode[] FoundNodes = null;
foreach (string folder in strFolders) {
if (!folder.Contains(":")) {
if (CurrentNode == null) {
FoundNodes = treeFolders.Nodes.Find(folder, false);
} else {
FoundNodes = CurrentNode.Nodes.Find(folder, false);
}
if (FoundNodes.Length > 0) {
CurrentNode = FoundNodes[0];
CurrentNode.Expand();
} else {
//no folder found. cant continue
break;
}
}
}
if (CurrentNode != null) {
treeFolders.SelectedNode = CurrentNode;
}
I need help on how to map my drives into a tree view. I can get my drives to show but I can not get all the sub directories. I would like to get my entire drive mapped out into a tree view. Any help would be appreciated. This is what I got so far.
string[] drives = Environment.GetLogicalDrives();
foreach (string dr in drives)
{
TreeNode node = new TreeNode(dr);
node.Tag = dr;
treeView1.Nodes.Add(node);
}
treeView1.CollapseAll();
To do it recursively:
private void fillTree() { // you allready had most of this
string[] drives = Environment.GetLogicalDrives();
foreach(string dr in drives) {
TreeNode node = RecursiveDirWalk(dr);
node.ImageIndex = 0; // drive icon
node.Tag = dr;
treeView1.Nodes.Add(node);
}
}
// now add this
private TreeNode RecursiveDirWalk(string path) {
TreeNode node = new TreeNode(path.Substring(path.LastIndexOf('\\')));
node.ImageIndex = 1; // dir icon
node.Tag = path;
string[] dirs = System.IO.Directory.GetDirectories(path);
for(int t = 0; t < dirs.Length; t++) {
node.Nodes.Add(RecursiveDirWalk(dirs[t]));
}
// Optional if you want files as well:
string[] files = System.IO.Directory.GetFiles(path);
for(int t = 0; t < files.Length; t++) {
TreeNode tn = new TreeNode(System.IO.Path.GetFileName(files[t]));
tn.Tag = files[t];
tn.ImageIndex = 2; // file icon
node.Nodes.Add(tn);
} // end of optional file part
return node;
}
Here's a lazy loading version:
private void fillTree() {
string[] drives = Environment.GetLogicalDrives();
foreach(string dr in drives) {
TreeNode node = new TreeNode(dr);
node.Tag = dr;
node.ImageIndex = 0; // drive icon
node.Tag = dr;
treeView1.Nodes.Add(node);
node.Nodes.Add(new TreeNode("?"));
}
treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);
}
void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
if((e.Node.Nodes.Count == 1) && (e.Node.Nodes[0].Text=="?")) {
RecursiveDirWalk(e.Node);
}
}
private TreeNode RecursiveDirWalk(TreeNode node) {
string path = (string)node.Tag;
node.Nodes.Clear();
string[] dirs = System.IO.Directory.GetDirectories(path);
for(int t = 0; t < dirs.Length; t++) {
TreeNode n = new TreeNode(dirs[t].Substring(dirs[t].LastIndexOf('\\')+1));
n.ImageIndex = 1; // dir icon
n.Tag = dirs[t];
node.Nodes.Add(n);
n.Nodes.Add(new TreeNode("?"));
}
// Optional if you want files as well:
string[] files = System.IO.Directory.GetFiles(path);
for(int t = 0; t < files.Length; t++) {
TreeNode tn = new TreeNode(System.IO.Path.GetFileName(files[t]));
tn.Tag = files[t];
tn.ImageIndex = 2; // file icon
node.Nodes.Add(tn);
} // end of optional file part
return node;
}
Here's the code used to display a tree with only the drives shown at first, adding to the tree as the user clicks on the nodes. This code will not display empty drives. Adding icons to each node is left as an exercise for the student:
private static string PLACEHOLDER = "...";
private void ListDrives()
{
string[] drives = Environment.GetLogicalDrives();
foreach (string drive in drives)
{
if (Directory.Exists(drive))
{
TreeNode node = new TreeNode(drive);
node.Tag = drive;
this.treeViewFolders.Nodes.Add(node);
node.Nodes.Add(new TreeNode(PLACEHOLDER));
}
}
this.treeViewFolders.BeforeExpand += new TreeViewCancelEventHandler(treeView_BeforeExpand);
}
void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Nodes.Count > 0)
{
if (e.Node.Nodes[0].Text == PLACEHOLDER)
{
e.Node.Nodes.Clear();
string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());
foreach (string dir in dirs)
{
DirectoryInfo di = new DirectoryInfo(dir);
TreeNode node = new TreeNode(di.Name);
node.Tag = dir;
try
{
if (di.GetDirectories().GetLength(0) > 0)
node.Nodes.Add(null, PLACEHOLDER);
}
catch (UnauthorizedAccessException)
{
// TODO: update node images
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ExplorerForm", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
e.Node.Nodes.Add(node);
}
}
}
}
}
I'm trying to populate a treeview from a list of folder path, for example:
C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\AppPatch\MUI\040C
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409
with an ouput like this:
├───addins
├───AppPatch
│ └───MUI
│ └───040C
├───Microsoft.NET
│ └───Framework
│ └───v2.0.50727
│ └───MUI
│ └───0409
Notice there's no 'C:\WINDOWS\Microsoft.NET' or 'C:\WINDOWS\Microsoft.NET\Framework' in the list. I've been working on this for almost two days and there's a bunch of bug in my code. Hope I can get help from here.
Thanks.
Eric
private void Form1_Load(object sender, EventArgs e)
{
var paths = new List<string>
{
#"C:\WINDOWS\AppPatch\MUI\040C",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
treeView1.PathSeparator = #"\";
PopulateTreeView(treeView1, paths, '\\');
}
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
}
}
for a linq'y version:
public static TreeNode MakeTreeFromPaths(List<string> paths, string rootNodeName = "", 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;
}
ehosca answer is correcr, but there is a little problem,
when I change paths to like this
C:\WINDOWS\AppPatch\MUI\040C
D:\WIS\Microsoft.NET\Framework\v2.0.50727
E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409
It will popoulate treeview like this.
But adding some extra code we can avoid this situation. So I changed the code in PopulateTreeView
private static void PopulateTreeView(TreeView treeView, string[] paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
lastNode = null; // This is the place code was changed
}
}
Now it works fine like this
I took your code, and it work very well,
but i made just a little modification for improving the load speed
when it is used whit a large list of files
it seems like find operation, and string operations generally are very slow
private TreeNode PopulateTreeNode2(string[] paths, string pathSeparator)
{
if (paths == null)
return null;
TreeNode thisnode = new TreeNode();
TreeNode currentnode;
char[] cachedpathseparator = pathSeparator.ToCharArray();
foreach (string path in paths) {
currentnode = thisnode;
foreach (string subPath in path.Split(cachedpathseparator))
{
if (null == currentnode.Nodes[subPath])
currentnode = currentnode.Nodes.Add(subPath, subPath);
else
currentnode = currentnode.Nodes[subPath];
}
}
return thisnode;
}
then you can use:
string[] paths = {
#"C:\WINDOWS\AppPatch\MUI\040C",
#"D:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
TreeView treeview = new TreeView();
treeview.Nodes.Add(PopulateTreeNode2(paths, "\\"));
NOTE: maybe some string sensitivity check will be needed in both solutions,
in order to prevent some folders re creations.
because some url could be pointing to the same folder on the disk
but spelled different such as:
Windows ; WinDOWs , WINDOWS
Here's some very old code I once used to create an ASP.NET treeview from code (assuming TreeView has an ID of TreeViewFolders):
protected void Page_Load(object sender, EventArgs e)
{
GenerateTreeView(#"C:\WINDOWS\");
}
private void GenerateTreeView(string rootPath)
{
GetFolders(System.IO.Path.GetFullPath(rootPath), TreeViewFolders.Nodes);
TreeViewFolders.ExpandDepth = 1;
}
// recursive method to load all folders and files into tree
private void GetFolders(string path, TreeNodeCollection nodes)
{
// add nodes for all directories (folders)
string[] dirs = Directory.GetDirectories(path);
foreach (string p in dirs)
{
string dp = p.Substring(path.Length);
nodes.Add(Node("", p.Substring(path.Length), "folder"));
}
// add nodes for all files in this directory (folder)
string[] files = Directory.GetFiles(path, "*.*");
foreach (string p in files)
{
nodes.Add(Node(p, p.Substring(path.Length), "file"));
}
// add all subdirectories for each directory (recursive)
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].Value == "folder")
GetFolders(dirs[i] + "\\", nodes[i].ChildNodes);
}
}
// create a TreeNode from the specified path, text and type
private TreeNode Node(string path, string text, string type)
{
TreeNode n = new TreeNode();
n.Value = type;
n.Text = text;
return n;
}
private void Form2_Load(object sender, EventArgs e)
{
treeView1.CheckBoxes = true;
foreach (TreeNode node in treeView1.Nodes)
{
node.Checked = true;
}
string[] drives = Environment.GetLogicalDrives();
foreach (string drive in drives)
{
// treeView1.Nodes[0].Nodes[1].Checked = true;
DriveInfo di = new DriveInfo(drive);
int driveImage;
switch (di.DriveType)
{
case DriveType.CDRom:
driveImage = 3;
break;
case DriveType.Network:
driveImage = 6;
break;
case DriveType.NoRootDirectory:
driveImage = 8;
break;
case DriveType.Unknown:
driveImage = 8;
break;
default:
driveImage = 2;
break;
}
TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
node.Tag = drive;
if (di.IsReady == true)
node.Nodes.Add("...");
treeView1.Nodes.Add(node);
}
foreach (TreeNode node in treeView1.Nodes)
{
node.Checked = true;
}
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
{
if (e.Node.Nodes.Count > 0)
{
if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
{
e.Node.Nodes.Clear();
string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());
foreach (string dir in dirs)
{
DirectoryInfo di = new DirectoryInfo(dir);
TreeNode node = new TreeNode(di.Name, 0, 1);
node.Checked = true;
try
{
node.Tag = dir;
if (di.GetDirectories().Count() > 0)
node.Nodes.Add(null, "...", 0, 0).Checked = node.Checked;
}
catch (UnauthorizedAccessException)
{
node.ImageIndex = 12;
node.SelectedImageIndex = 12;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "DirectoryLister", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
node.Checked = e.Node.Checked;
e.Node.Nodes.Add(node);
}
}
}
}
}
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
button1.Enabled = false;
TreeNode node = e.Node;
bool is_checked = node.Checked;
foreach (TreeNode childNode in e.Node.Nodes)
{
childNode.Checked = e.Node.Checked;
}
treeView1.SelectedNode = node;
}
I've managed to create a tree from path list using only for cycles. And it looks like it is the simpliest answer of this question at this moment.
Use this block of code to create a tree. list is the list of your files or folders, treeView1 is your TreeView.
//Creates a tree from given path list
foreach (string path in list)
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (string path_part in path.Split('\\'))
{
//Here it adds a new node (file or folder)
if (!nodes.ContainsKey(path_part))
nodes.Add(path_part, path_part);
//Go one node deeper
nodes = nodes[path_part].Nodes;
}
}
Note - this may break if you use it with paths that start with the path separator (e.g. /home/user)
And if you want to remove common parts of your paths (or remove single parent nodes), use this block of code right after the previous one:
//This removes "single" TreeNodes (common paths)
while (treeView1.Nodes.Count == 1)
{
//This "unpacks" child TreeNodes from the only parent TreeNode
for (int i = 0; i < treeView1.Nodes[0].Nodes.Count; i++)
treeView1.Nodes.Add(treeView1.Nodes[0].Nodes[i]);
//This removes parent TreeNode
treeView1.Nodes.RemoveAt(0);
}
This will generate an empty tree if all of your paths are the same.