views:

196

answers:

3

Hello,

I work in the Systems & admin team and have been given the task of creating a quota management application to try and encourage users to better manage there resources as we currently have issues with disc space and don't enforce hard quotas.

At the moment I'm using the code below to go through all the files in a users homespace to retrieve the overall amount of space they are using. As from what I've seen else where theres no other way to do this in C#, the issue with it is theirs quite a high overhead while it retireves the size of each file then creates a total.

try
{
    long dirSize = 0;
    FileInfo[] FI = new DirectoryInfo("I:\\").GetFiles("*.*", SearchOption.AllDirectories);
    foreach (FileInfo F1 in FI)
    {
        dirSize += F1.Length;
    }

    return dirSize;
}

So I'm looking for a quicker way to do this or a quick way to monitor changes in the size of files while using the options avaliable through FileSystemWatcher. At the moment the only thing I can think of is creating a hashtable containing the file location and size of each file, so when a size changed event occurs I can compare the old size against the new one and update the total.

Any suggestions would be greatly appreciated.

+3  A: 

as far as trying to code your own management system, the table of location and size isn't a bad idea. maybe incorporate some xmlserialization and linq to xml for reporting, etc. I think you're already on the right path by deciding to use FileSystemWatcher

David
Thanks for the feedback, its appreciated, just glad to hear I'm going down the right path. Can I just clarify that what your suggestion is regaridng XML, are you suggesting I create a local location to store the information rather than a table then perform LINQ to XML queries based on that?I already have plans for some management such as a central XML store where details will be reported back with some sort of management console, but the main goal initially will be to create a client which can then be expanded.
manemawanna
yes, that's what im suggesting. i can't really see a need (doesn't mean you can't find one later) for a whole database to manage what you're requesting. as far as i can tell, you will really only need one xml file for storage (or "table" in a database)--not much point in a database with a single table
David
+2  A: 

I agree with David, using FileSystemWatcher means you'll only have to compute when the directory changes.

When the directory contents changed, just work out what has changed and go from there.

Use the Changed event.

AndyC
Thanks for the response thats pretty much what I've started doing at the moment, this is very early development so only started yesterday. At the moment I have 2 event handlers, one to control new files being created, one to handle changes in file size and I should be adding another to deal with file deletions.
manemawanna
+1  A: 

You do know you can enforce quotas on NTFS volumes via Windows? Why roll your own when the OS supports this natively?

Dan Diplo
Yep I'm aware of this, but this is to get users to be more proactive in maintaining their home area and shared drives, not keeping multiple copies of files etc and give them gentle reminders. Before the application fills up most of the screen and gives them options for files to delete.
manemawanna