Help file browser SLOW!!!

Wing

Member
Joined
May 13, 2007
Messages
15
Location
Sweden
Hi well i made my first try to make a file browser but its SLOW it might be the logic im using...

is there any faster better way to make this work? would be greatfull for any help!!!

here is the code i wrote:
Code:
private void Form1_Load(object sender, EventArgs e)
        {
            AddDrivers(DrivertreeView);
        }

        private void AddDrivers(TreeView TreeViewControl)
        {
            DriveInfo[] DriverInfos = DriveInfo.GetDrives();
            foreach (DriveInfo Driveinfo in DriverInfos)
            {
                if (Driveinfo.Name != @"A:\")
                {

                    Icon i = ShellIcon.GetSmallIcon(Driveinfo.Name);
                    SmallIconimageList.Images.Add(Driveinfo.Name, i);

                    TreeNode node = TreeViewControl.Nodes.Add(Driveinfo.Name, Driveinfo.Name, Driveinfo.Name, Driveinfo.Name);

                    DirectoryInfo Directoryinfo = new DirectoryInfo(Driveinfo.Name);
                    try
                    {
                        AddFolders(TreeViewControl, Directoryinfo, node);

                    }
                    catch (Exception E)
                    {
                        Trace.WriteLine(E.Message);
                    }
                }
            }
        }

        private void AddFolders(TreeView TreeViewControl, DirectoryInfo directory,TreeNode node)
        {
            DirectoryInfo[] DirectoryInfos;
            try
            {
                DirectoryInfos = directory.GetDirectories();
            }
            catch(Exception)
            {
                return;
            }
            foreach (DirectoryInfo DI in DirectoryInfos)
            {
                Trace.WriteLine(DI.FullName);
                Icon i = ShellIcon.GetSmallIcon(DI.FullName);
                SmallIconimageList.Images.Add(DI.FullName, i);
                node.Nodes.Add(DI.Name, DI.Name, DI.FullName, DI.FullName);
            }
        }

        private void AddFiles(ListView listView,DirectoryInfo directory)
        {
            listView.Items.Clear();
            FileInfo[] Files = directory.GetFiles();
            foreach (FileInfo file in Files)
            {
                Icon i = ShellIcon.GetSmallIcon(file.FullName);
                SmallIconimageList.Images.Add(file.FullName, i);
                i = ShellIcon.GetLargeIcon(file.FullName);
                LargeIconimageList.Images.Add(file.FullName, i);
                listView.Items.Add(file.Name, file.FullName);
            }
        }

        private void DrivertreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            foreach (TreeNode node in e.Node.Nodes)
            {
                Trace.WriteLine(node.FullPath);
                AddFolders(DrivertreeView, new DirectoryInfo(node.FullPath), node);
            }
            
        }

        private void DrivertreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            AddFiles(FileslistView, new DirectoryInfo(e.Node.FullPath));
        }
 

Attachments

Last edited by a moderator:
You could wrap the slower parts of the screen updating code between a SuspendLayout() and a ResumeLayout() - that should give a bit of a performance boost.
C#:
        private void AddFiles(ListView listView, DirectoryInfo directory)
        {
            listView.Items.Clear();
            FileInfo[] Files = directory.GetFiles();
            try
            {
                SuspendLayout();
                foreach (FileInfo file in Files)
                {
                    Icon i = ShellIcon.GetSmallIcon(file.FullName);
                    SmallIconimageList.Images.Add(file.FullName, i);
                    i = ShellIcon.GetLargeIcon(file.FullName);
                    LargeIconimageList.Images.Add(file.FullName, i);
                    listView.Items.Add(file.Name, file.FullName);
                }
            }
            finally
            {
                ResumeLayout();
            }
        }

Other than that you could do similar to explorer - load the file names and a dummy icon and then load the icons in a background thread.
 
Making a file broswer in C# is tricky; Ive tried it. One problem is that using the built in ShellIcon class you have no way of knowing when icons can be re-used and you end up allocating a massive amount of duplicate icons, which takes time to load and puts a large burden on the garbage collector. A better, albeit very much more complex solution would be to use the Windows API, which provides a method of getting the icons that can avoid loading duplicates. You would have to do some research, however, because I dont have an example and I dont know the specifics. Another recommendation might be to avoid using the DirectoryInfo class (sticking with the simpler Directory class might be faster and easier on the GC).

When I tried to make my own browser, what I found most effective for speeding things up was, firstly, using a background thread that pre-cached data before the user requested it, and secondly, rendering with GDI instead of GDI+ (I was doing my own rendering).
 
Back
Top