File size monitoring in C#
Posted
by manemawanna
on Stack Overflow
See other posts from Stack Overflow
or by manemawanna
Published on 2010-04-16T13:30:36Z
Indexed on
2010/04/16
13:33 UTC
Read the original article
Hit count: 226
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.
© Stack Overflow or respective owner