开发者

File size monitoring in C#

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开发者_StackOverflow.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜