tags:

views:

216

answers:

6

Hi

I have built a small app that allows me to choose a directory and count the total size of files in that directory and its sub directories.

It allows me to select a drive and this populates a tree control with the drives immediate folders which I can then count its size!

It is written in .net and simply loops round on the directories and for each directory adds up the file sizes.

It brings my pc to a halt when It runs on say the windows or program files folders.

I had thought of Multi threading but I haven't done this before.

Any ideas to increase performance?

thanks

A: 

Do you want to increase performance or increase system responsiveness?

You can increase RESPONSIVENESS by instructing the spidering application to run its message queue loop periodically, which handles screen repaints, etc. This would allow you to give a progress update as it executes the scan, while actually decreasing performance (because you're yielding CPU priority).

Tetsujin no Oni
both! I have never built an application like this so just looking for some help/pointers! thanks
Rigobert Song
A: 

This gets sub-directories:

 string[] directories = Directory.GetDirectories(node.FullPath);
                    foreach (string dir in directories)
                    {
                        TreeNode nd = node.Nodes.Add(dir, dir.Substring(dir.LastIndexOf("\\")).Replace("\\", ""), 3);
                        if (showItsChildren)
                            ShowChildDirectories(nd, true);
                        size += GetDirectorySize(nd.FullPath);
                    }

Thsi counts file sizes:

long b = 0;

                // Get array of all file names.
                string[] a = Directory.GetFiles(p, "*.*");

                // 2
                // Calculate total bytes of all files in a loop.
                foreach (string name in a)
                {
                    // 3
                    // Use FileInfo to get length of each file.
                    FileInfo info = new FileInfo(name);
                    b += info.Length;
                    IncrementCount();
                }
Rigobert Song
A: 

Try to comment out all the parts that update the UI, if it's still slow it's the disk I/O and there's nothing you can do, if it gets faster you can update the UI every X files to save UI work.

You can make your UI responsive by doing all the work in a worker thread, but it will make it slightly slower.

Disk I/O is relatively slow it is also often needed by other applications (swap file, temp files ...) also, multi-threading won't help much, all the file are on the same physical disk and it's likely the disk I/O is the bottleneck.

Nir
A: 

Just a guess, but I bet your performance hit involves the UI and not the file scan. Comment out the code that creates the TreeNode.

Try to not make your tree paint until after you complete your scan:

Make sure that the root tree node for all of your files is NOT added to the treey. Add all the children, and then add the "top" node/nodes at the very end of your processing. See how that works.

JMarsch
+3  A: 

Your code is really going to slog since you're just using strings to refer to directories and files. Use a DirectoryInfo on your root directory; get a list of FileSystemInfos from that one using DirectoryInfo.GetFileSystemInfos(); iterate on that list, recursing in for DirectoryInfo objects and just adding the size for FileInfo objects. That should be a LOT faster.

McWafflestix
+1  A: 

I'd simply suggest using a background worker to preform the work. You'll probably want to make sure controls that shouldn't be usable aren't but anything that would be usable could stay usable.

Google: http://www.google.com/search?q=background+worker

This would allow your application to be multi-threaded with out some of the complexity of multiple threads. Everything has been packaged up and it convenient to use.

Frank V