Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

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));
       }

WindowsApplication4.zip

Edited by PlausiblyDamp
  • Administrators
Posted

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.

       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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

Making a file broswer in C# is tricky; I've 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 don't have an example and I don't 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).

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...