FileSystemWatcher监视目录大小

时间:2012-04-27 08:26:54

标签: c# windows-services filesystemwatcher

您好我正在创建一个Windows服务来查看某些目录,以查看目录的大小是否达到其限制。 我创建了一个文件系统观察器如下

            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = dirPaths[i].ToString();
            watcher.NotifyFilter = NotifyFilters.Size;
            watcher.EnableRaisingEvents = true;
            watcher.Changed += new FileSystemEventHandler(OnChanged);

 private void OnChanged(object source, FileSystemEventArgs e)
    {
        try
        {


        string directory = new DirectoryInfo(e.FullPath).Parent.FullName;//gettting the directory path from the full path

        float dirSize = CalculateFolderSize(directory);

        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
        }
    }
    catch (Exception ex)
    {
        eventLogCheck.WriteEntry(ex.ToString());
    }

}

CalculateFolderSize检查驱动器中所有文件和子目录的大小。

现在,当我将文件添加到目录eq a .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;
        }

1 个答案:

答案 0 :(得分:5)

您正在使用FileSystemEventArgs提供的文件夹路径,因此这将是已更改的文件夹。相反,为您正在监视的每个目录根创建一个对象,并在其中存储根路径并使用该路径而不是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
    }
 };