Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TreeView Question (Folder Browser)

TreeView Question (Folder Browser)

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QzRz
    wrote on last edited by
    #1

    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

    R A 2 Replies Last reply
    0
    • Q 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

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      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 call AddDirectory(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.

      A Q 2 Replies Last reply
      0
      • Q 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

        A Offline
        A Offline
        Allan Eagle
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R Robert Rohde

          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 call AddDirectory(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.

          A Offline
          A Offline
          Allan Eagle
          wrote on last edited by
          #4

          Robert, Snap. Your way of dynamically creating the tree of course would be better ... Allan

          1 Reply Last reply
          0
          • R Robert Rohde

            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 call AddDirectory(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.

            Q Offline
            Q Offline
            QzRz
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • Q QzRz

              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

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups