An open source drive space viewer

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi all,

I am looking to make an open source Drive space viewer. One that displays
the sizes in a graph and lets you move through the directories via a
treeview at the left.

I have three questions i hope someone could help with:

1. The problem I have is tallying up the directories and sub
directories. This is made even more complicated by the fact that I need to
allow the user to exclude certain sub directories when doing the
calculations. Does anyone know if there is a class in the framework that, at
the very least, can tell me the size of a folder and its contents.

2. Does anyone have any code or suggestions about how I could handle the
problem of excluding various directories. The user needs to click a tick box
in the treeview to have that dir included

3. Does anyone know of an open source application that I could look at for
this purpose

Thank you everyone

Simon
 
Simon,

1) .NET Framework has two classes that you can use to obtain the
information specific to files and directories. They are: FileInfo and
DirectoryInfo. Unfortunately, there is not a method on the
DirectoryInfo class to return the size of the Directory. I wrote a
little helper class that has a method on it that will return the size
of the directory in bytes. Here it is:

public class FileSystemHelper
{
private FileSystemHelper()
{
}
// returns the size of the given directory in bytes.
public static long GetDirectorySize(DirectoryInfo di)
{
if(di==null)
{
throw new ArgumentNullException("di");
}
long size = 0;
FileInfo[] files = di.GetFiles();
// sum up file size
if(files!=null && files.Length>0)
{
foreach(FileInfo fi in files)
{
size+=fi.Length;
}
}
// sum up dir size
DirectoryInfo[] dirs = di.GetDirectories();
if(dirs!=null && dirs.Length>0)
{
foreach(DirectoryInfo dii in dirs)
{
// recursive call
size+=FileSystemHelper.GetDirectorySize(dii);
}
}
return size;
}

}

You can get to the contents of the directory by calling GetFiles() and
GetDirectories().

2) GetFiles() and GetDirectories() is overloaded to accept a
searchPattern so you can use that to exclude certain
files/directories.

3) I don't know of any open source tools that do this kind of stuff.

sayed
 
Back
Top