C# 用于监视目录大小的FileSystemWatcher
您好,我正在创建一个windows服务来监视某些目录,以查看目录的大小是否达到其限制。 我创建了一个文件系统监视程序,如下所示:C# 用于监视目录大小的FileSystemWatcher,c#,windows-services,filesystemwatcher,C#,Windows Services,Filesystemwatcher,您好,我正在创建一个windows服务来监视某些目录,以查看目录的大小是否达到其限制。 我创建了一个文件系统监视程序,如下所示: FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = dirPaths[i].ToString(); watcher.NotifyFilter = NotifyFilters.Size; wa
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = dirPaths[i].ToString();
watcher.NotifyFilter = NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
watcher.Changed += new FileSystemEventHandler(OnChanged);
及
CalculateFolderSize
检查驱动器中所有文件和子目录的大小
现在,当我向目录中添加一个文件(例如.xls、.txt等)时,这可以正常工作,但如果我向目录中添加一个文件夹,它不会触发OnChanged
事件
如果我启用:
watcher.IncludeSubdirectories = true;
它确实会触发Onchanged
事件,但在这种情况下,它只检查子目录,而不是整个目录
请有人告诉我如何使其工作,以便当我将文件夹复制到正在监视的目录时,它会触发Onchanged
事件并计算目录的新大小
如果有帮助,我的CalculateFolderSize
函数如下所示:
//function to calculate the size of the given path
private float CalculateFolderSize(string folder)
{
float folderSize = 0.0f;
try
{
//Checks if the path is valid or not
if (!Directory.Exists(folder))
{
return folderSize;
}
else
{
try
{
foreach (string file in Directory.GetFiles(folder))
{
if (File.Exists(file))
{
FileInfo finfo = new FileInfo(file);
folderSize += finfo.Length;
}
}
foreach (string dir in Directory.GetDirectories(folder))
{
folderSize += CalculateFolderSize(dir);
}
}
catch (NotSupportedException ex)
{
eventLogCheck.WriteEntry(ex.ToString());
}
}
}
catch (UnauthorizedAccessException ex)
{
eventLogCheck.WriteEntry(ex.ToString());
}
return folderSize;
}
您正在使用
文件系统EventTargets
提供的文件夹路径,因此该文件夹已更改。相反,为正在监视的每个目录根创建一个对象,并在其中存储根路径,并使用该路径而不是EventArgs
您可能会发现,创建此对象的一种简单方法就是对事件处理程序使用lambda函数,如下所示。这有效地将外部作用域中的路径包装到您监视的每个路径的不同对象中
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = dirPaths[i].ToString();
watcher.NotifyFilter = NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
watcher.Changed += delegate (object source, FileSystemEventArgs e)
{
float dirSize = CalculateFolderSize(watcher.Path); // using the path from the outer scope
float limitSize = int.Parse(_config.TargetSize);//getting the limit size
if (dirSize > limitSize)
{
eventLogCheck.WriteEntry("the folloing path has crossed the limit " + directory);
//TODO: mail sending
}
};
这似乎是一个很好的解决方案,我相信它能起作用,但我对VS C是新手,所以当我放置watcher.Changed+=(sender,e)=>时,它说这是一个无效的表达式术语,帮助plsi也放置了一个;在lambda表达式的末尾,但它不起作用。您使用的是什么版本的C#?要使用lambda表达式,您需要3.0或更高版本。我已经对其进行了编辑,使其与2.0兼容,但您确实希望升级—您错过了很多!有没有办法找出哪个观察者调用了Onchanged事件
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = dirPaths[i].ToString();
watcher.NotifyFilter = NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
watcher.Changed += delegate (object source, FileSystemEventArgs e)
{
float dirSize = CalculateFolderSize(watcher.Path); // using the path from the outer scope
float limitSize = int.Parse(_config.TargetSize);//getting the limit size
if (dirSize > limitSize)
{
eventLogCheck.WriteEntry("the folloing path has crossed the limit " + directory);
//TODO: mail sending
}
};