CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Location
    Detroit MI
    Posts
    80

    File Explorer - Tree View Style

    Hi,

    I have the following code (at bottom of thread). All works fine, but it takes about 26 seconds to run through.

    The function 'FillDirectoryNodes' is reentrant in order to populate the treeview with all directories on the HDD.

    My question is, how can I speed this up?

    Also, which is the part that takes the longest? Creating the treeview node, getting the directory info, etc...?

    Thanks.

    Code:
    public partial class FileExplorer
        {
            public FileExplorer()
            {
            }
    
            public void BuildFileExplorerTreeView(TreeView fe)
            {
                /* Clear the treeview control */
                fe.Nodes.Clear();
    
                /* Search the system for storage drives */
                DriveInfo[] allDrives = DriveInfo.GetDrives();
    
                /* For each drive, fill it's files and folders in the treeview */
                foreach (DriveInfo currentDrv in allDrives)
                {
                    if (currentDrv.DriveType == DriveType.Fixed)
                    {
                        TreeNode node = new TreeNode();
                        node.Text = currentDrv.Name.ToString();
                        fe.Nodes.Add(node);
                        FillDirectoryNodes(currentDrv.Name.ToString(), node);
                    }
                }
            }
    
    
            private void FillDirectoryNodes(string drv, TreeNode parentNode)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(drv);
    
                try
                {
                    if (dirInfo.Exists)
                    {
                        foreach (DirectoryInfo subDirInfo in dirInfo.GetDirectories())
                        {
                            if (subDirInfo.Exists)
                            {
                                TreeNode node = new TreeNode();
                                node.Text = subDirInfo.Name;
                                parentNode.Nodes.Add(node);
                                FillDirectoryNodes(node.FullPath, node);
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
    }
    Regards,

    Big Winston

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: File Explorer - Tree View Style

    Quote Originally Posted by BigWinston
    My question is, how can I speed this up?
    Don't load the complete drive. Load one 'level' at the time.

    Also, which is the part that takes the longest? Creating the treeview node, getting the directory info, etc...?
    Harddrives etc. are still not that quick, neither are 'massive' GUI oprerations.

    Before you do alot of GUI updating (adding nodes, repositioning controls etc.) you should stop rendering; SuspendLayout. Then when you're don't you call ResumeLayout to make the control(s) update/render themselves.

    - petter

  3. #3
    Join Date
    Aug 2003
    Location
    London
    Posts
    515

    Re: File Explorer - Tree View Style

    Quote Originally Posted by wildfrog
    Don't load the complete drive. Load one 'level' at the time.
    Yeah, definitely do this. Just load up the information when the user requests it, you can do this in the BeforeExpand event of your treeview...
    If it helped, then please rate the post by clicking "Rate this post"!

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

Featured