こんにちは私は、特定のディレクトリを監視して、ディレクトリのサイズが制限に達しているかどうかを確認する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 following path has crossed the limit " + directory);
//TODO: mail sending
}
}
catch (Exception ex)
{
eventLogCheck.WriteEntry(ex.ToString());
}
}
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;
}