Results 1 to 9 of 9

Thread: [2.0] Populate Treeview with all Directories

  1. #1

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    [2.0] Populate Treeview with all Directories

    I need to load all the directories in my C:\ into a TreeView. What would be the most efficient way? Also i need to only look for all image files.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Populate Treeview with all Directories

    The IO.Directory.GetDirectory method gets all subfolder paths in a folder and the GetFiles method gets all file paths with an optional filter. A little recursive function calling those two and you're done.

    Note that Directory.GetFiles has an option for a recursive search but it will choke on the inaccessible folder it encounters. With your own recursive method you catch exceptions on inaccessible folders and continue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2.0] Populate Treeview with all Directories

    IO.Directory.GetDirectories* ?

    Kk thanks ill have a look

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Populate Treeview with all Directories

    Quote Originally Posted by Paul M
    IO.Directory.GetDirectories* ?
    Yeah. Sorry about that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2.0] Populate Treeview with all Directories

    Looks like its going to be a little harder than expected -.-

    I would like to have a folder icon near all the folders/subfolders and a check box near all the files. But first things first i have to get everything listing properly. Haven't had time this morning to work on it.

    I am messing about a little and i am getting the following error on my second loop.
    Access to the path 'C:\System Volume Information' is denied.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Populate Treeview with all Directories

    What did I say in my previous post:
    Quote Originally Posted by jmcilhinney
    Note that Directory.GetFiles has an option for a recursive search but it will choke on the inaccessible folder it encounters. With your own recursive method you catch exceptions on inaccessible folders and continue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2.0] Populate Treeview with all Directories

    You should have bolded/underlined it in the first place

    Anyway i think i have it now thanks ill post back when its done or if i need help.

  8. #8
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Populate Treeview with all Directories

    I made this class for you. I think it works, but I'm not exactly sure.

    Code:
        /// <summary>
        /// Provides methods for recursively enumerating through all directories and files in the given directory and adds each item to a treeview.
        /// </summary>
        class DirectoryTree
        {
            private TreeView treeView;
            private TreeNode currentDirectory, previousDirectory;
            private DirectoryInfo directoryInfo;
    
            /// <summary>
            /// Initializes a new instance of the DirectoryTree class.
            /// </summary>
            /// <param name="treeView">The treeview that will have items added to it.</param>
            /// <param name="directory">The initial directory to start recursively searching through.</param>
            public DirectoryTree(TreeView treeView, string directory)
            {
                this.treeView = treeView;
                this.currentDirectory = treeView.Nodes.Add(directory);
                this.previousDirectory = currentDirectory;
                this.directoryInfo = new DirectoryInfo(directory);
            }
    
            /// <summary>
            /// Recursively enumerates and walks through each directory and file in the initial directory and adds each item to the treeview.
            /// </summary>
            public void WalkDirectory()
            {
                WalkDirectory(directoryInfo);
            }
    
            private void WalkDirectory(DirectoryInfo directory)
            {
                foreach (FileInfo file in directory.GetFiles())
                {
                    if (file.Extension.ToLower() == ".png")
                    {
                        currentDirectory.Nodes.Add(Path.GetFileNameWithoutExtension(file.Name));
                    }
                }
    
                DirectoryInfo[] subDirectories = directory.GetDirectories();
    
                foreach (DirectoryInfo subDirectory in subDirectories)
                {
                    currentDirectory = currentDirectory.Nodes.Add(subDirectory.Name);
    
                    WalkDirectory(subDirectory);
    
                    currentDirectory = previousDirectory;
                }
            }
        }

  9. #9
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: [2.0] Populate Treeview with all Directories

    Personally I use a "load on demand" directory treeview.

    It only populates child nodes when you expand a node, so no recursion is needed and it fast enough for my purposes. However, you may need more functionality than this.

    Anyway, I have attached a sample project, hope I removed all the binaries, that demostrates this approach.

    You stated that you need to show a folder next to each directory and a checkbox next to each file, so you'll have to modify my sample to do this as well as load the files in the tree, but this should be easy.

    Hope this helps,
    CT
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width