C# 中的文件大小监控
我在系统与技术部门工作。管理团队并被赋予创建配额管理应用程序的任务,以尝试鼓励用户更好地管理资源,因为我们目前存在磁盘空间问题并且不强制执行硬配额。
目前,我正在使用下面的代码来浏览用户主空间中的所有文件,以检索他们正在使用的空间总量。从我所看到的情况来看,在 C# 中没有其他方法可以做到这一点,它的问题是它们的开销相当高,同时它会取消每个文件的大小然后创建总计。
try
{
long dirSize = 0;
FileInfo[] FI = new DirectoryInfo("I:\\").GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo F1 in FI)
{
dirSize += F1.Length;
}
return dirSize;
}
因此,我正在寻找一种更快的方法来执行此操作,或者一种在使用 FileSystemWatcher 提供的选项时监视文件大小变化的快速方法。目前我唯一能想到的就是创建一个包含文件位置和每个文件大小的哈希表,因此当发生大小更改事件时,我可以将旧大小与新大小进行比较并更新总数。
任何建议将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就尝试编写自己的管理系统而言,位置和大小表并不是一个坏主意。也许合并一些 xmlserialization 和 linq to xml 用于报告等。我认为您已经走在正确的道路上,决定使用 FileSystemWatcher
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 的观点,使用 FileSystemWatcher 意味着您只需在目录更改时进行计算。
当目录内容更改时,只需找出更改的内容并从那里开始。
使用
Changed
事件。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.您确实知道可以通过 Windows 对 NTFS 卷实施配额< /a>?当操作系统本身支持此功能时,为什么要自己开发呢?
You do know you can enforce quotas on NTFS volumes via Windows? Why roll your own when the OS supports this natively?