TreeView Question (Folder Browser)
-
Hi, I am working on a Folder Browser for a program, my only guess of what to use is TreeView, but i have never used it before. I have searched on Codeproject for similar application that has a FolderBrowser, but they seems to advanced for me. Can anyone tell/show me how to make such a program. Though i do not have a clue on how to do, i started by doing this: private System.Windows.Forms.TreeView _Directory = new System.Windows.Forms.TreeView(); private void AddTo_Directory() { DirectoryInfo all = new DirectoryInfo("c:\\"); foreach(DirectoryInfo x in all.GetDirectories()) { this._Directory.Nodes.Add(x.ToString()); } } but that just give me the directories in "c:\\", I have no idea on how to get all the sub directories! :( Thanks QzRz QzRz
-
Hi, I am working on a Folder Browser for a program, my only guess of what to use is TreeView, but i have never used it before. I have searched on Codeproject for similar application that has a FolderBrowser, but they seems to advanced for me. Can anyone tell/show me how to make such a program. Though i do not have a clue on how to do, i started by doing this: private System.Windows.Forms.TreeView _Directory = new System.Windows.Forms.TreeView(); private void AddTo_Directory() { DirectoryInfo all = new DirectoryInfo("c:\\"); foreach(DirectoryInfo x in all.GetDirectories()) { this._Directory.Nodes.Add(x.ToString()); } } but that just give me the directories in "c:\\", I have no idea on how to get all the sub directories! :( Thanks QzRz QzRz
You will have to make a recursive algorithm:
private void AddDirectory(DirectoryInfo dir, TreeNodeCollection col) { TreeNode node = col.Add(dir.Name); try { foreach(DirectoryInfo x in dir.GetDirectories()) AddDirectory(x, node.Nodes); } catch {} }
Now you could callAddDirectory(new DirectoryInfo("I:\\"), _tree.Nodes);
to fill the tree with the complete hierarchie. The problem that arises is that this will load the entire folder structure of your hdd to the treeview at once. This would probably be frustrating to the user. To avoid this you should always onle fill into the tree what you have to. So your first step isnt that wrong, because you are filling only the subfolders of the first hierarchie. To add needed subfolders when a node is expanded you will need to catch the AfterExpand event of the treeview and add the needed subfolders to the nodes in the expanded node. The following functions should work to do this:private void AddParent(string dir) { DirectoryInfo dirInfo = new DirectoryInfo(dir); TreeNode node = _tree.Nodes.Add(dirInfo.Name); node.Tag = dirInfo; GetDirectories(dirInfo, node.Nodes); } private void GetDirectories(DirectoryInfo dir, TreeNodeCollection col) { try { foreach(DirectoryInfo x in dir.GetDirectories()) { TreeNode node = col.Add(x.Name); node.Tag = x; } } catch {} } private void Tree_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e) { foreach (TreeNode node in e.Node.Nodes) GetDirectories((DirectoryInfo)node.Tag, node.Nodes); }
Tree_AfterExpand must be set as the eventhandler of the corresponding event of your treeview. For each drive you want to add there must be one call to AddParent. -
Hi, I am working on a Folder Browser for a program, my only guess of what to use is TreeView, but i have never used it before. I have searched on Codeproject for similar application that has a FolderBrowser, but they seems to advanced for me. Can anyone tell/show me how to make such a program. Though i do not have a clue on how to do, i started by doing this: private System.Windows.Forms.TreeView _Directory = new System.Windows.Forms.TreeView(); private void AddTo_Directory() { DirectoryInfo all = new DirectoryInfo("c:\\"); foreach(DirectoryInfo x in all.GetDirectories()) { this._Directory.Nodes.Add(x.ToString()); } } but that just give me the directories in "c:\\", I have no idea on how to get all the sub directories! :( Thanks QzRz QzRz
Looks like you need a bit of recursion:-
private void Form1_Load(object sender, System.EventArgs e) { AddDirectories(_Directory.Nodes, new DirectoryInfo("c:\\"), 1); } private void AddDirectories(TreeNodeCollection tn, DirectoryInfo dir, int level) { if (level <= 5) { try { DirectoryInfo[] dirs = dir.GetDirectories(); foreach(DirectoryInfo subDir in dirs) { TreeNode newNode = tn.Add(x.ToString()); newNode.Tag = subDir; AddDirectories(newNode.Nodes, subDir, level + 1); } } catch { // You may want to do something here if an error occurs } } }
The AddDirectories() method calls itself passing in DirectoryInfo and the new node for each new directory it finds and so on. The level variable in this example serves as a way to restrict how far into the directory tree the method should go. Also I usually store the DirectoryInfo into the tag of the newly created node so I can make use of it later when the node is selected by the user. -
You will have to make a recursive algorithm:
private void AddDirectory(DirectoryInfo dir, TreeNodeCollection col) { TreeNode node = col.Add(dir.Name); try { foreach(DirectoryInfo x in dir.GetDirectories()) AddDirectory(x, node.Nodes); } catch {} }
Now you could callAddDirectory(new DirectoryInfo("I:\\"), _tree.Nodes);
to fill the tree with the complete hierarchie. The problem that arises is that this will load the entire folder structure of your hdd to the treeview at once. This would probably be frustrating to the user. To avoid this you should always onle fill into the tree what you have to. So your first step isnt that wrong, because you are filling only the subfolders of the first hierarchie. To add needed subfolders when a node is expanded you will need to catch the AfterExpand event of the treeview and add the needed subfolders to the nodes in the expanded node. The following functions should work to do this:private void AddParent(string dir) { DirectoryInfo dirInfo = new DirectoryInfo(dir); TreeNode node = _tree.Nodes.Add(dirInfo.Name); node.Tag = dirInfo; GetDirectories(dirInfo, node.Nodes); } private void GetDirectories(DirectoryInfo dir, TreeNodeCollection col) { try { foreach(DirectoryInfo x in dir.GetDirectories()) { TreeNode node = col.Add(x.Name); node.Tag = x; } } catch {} } private void Tree_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e) { foreach (TreeNode node in e.Node.Nodes) GetDirectories((DirectoryInfo)node.Tag, node.Nodes); }
Tree_AfterExpand must be set as the eventhandler of the corresponding event of your treeview. For each drive you want to add there must be one call to AddParent.Robert, Snap. Your way of dynamically creating the tree of course would be better ... Allan
-
You will have to make a recursive algorithm:
private void AddDirectory(DirectoryInfo dir, TreeNodeCollection col) { TreeNode node = col.Add(dir.Name); try { foreach(DirectoryInfo x in dir.GetDirectories()) AddDirectory(x, node.Nodes); } catch {} }
Now you could callAddDirectory(new DirectoryInfo("I:\\"), _tree.Nodes);
to fill the tree with the complete hierarchie. The problem that arises is that this will load the entire folder structure of your hdd to the treeview at once. This would probably be frustrating to the user. To avoid this you should always onle fill into the tree what you have to. So your first step isnt that wrong, because you are filling only the subfolders of the first hierarchie. To add needed subfolders when a node is expanded you will need to catch the AfterExpand event of the treeview and add the needed subfolders to the nodes in the expanded node. The following functions should work to do this:private void AddParent(string dir) { DirectoryInfo dirInfo = new DirectoryInfo(dir); TreeNode node = _tree.Nodes.Add(dirInfo.Name); node.Tag = dirInfo; GetDirectories(dirInfo, node.Nodes); } private void GetDirectories(DirectoryInfo dir, TreeNodeCollection col) { try { foreach(DirectoryInfo x in dir.GetDirectories()) { TreeNode node = col.Add(x.Name); node.Tag = x; } } catch {} } private void Tree_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e) { foreach (TreeNode node in e.Node.Nodes) GetDirectories((DirectoryInfo)node.Tag, node.Nodes); }
Tree_AfterExpand must be set as the eventhandler of the corresponding event of your treeview. For each drive you want to add there must be one call to AddParent. -
Thanks alot... it helped me alot :D I only have one more question (I think)... Do you know if the "Environment" is the best way to get all the logical drives? Thanks QzRz QzRz
To determine if its the best way I would have to know ALL ways to get this information :-). The only other way I know of (beside direct api calls) is
System.IO.Directory.GetLogicalDrives
, but I dont know if there is any difference between the two.