ルート階層フォルダーだけを監視するのではなく、各フォルダーを個別に監視するアプリケーションを作成したい(監視サブフォルダーをオフにして)
ただし、削除ロジックを正しく取得できないようです。アプリケーションの内部で、または外部で、たとえばWindowsエクスプローラーを介して、階層内の任意のフォルダーを削除できるようにしたい。
これらのいずれかを試してみると、削除コマンドが実行に失敗するため、ロックの問題が発生するようです。
ウォッチングを無効にすると、すべてが機能しているように見えます。したがって、問題は監視されているフォルダを削除しようとしていることだと思います。
とにかくこれを回避する方法はありますか?理想的には、監視しているフォルダーが削除されたときに、fileSystemWatcherが自動的に監視を停止するようにします。
public MainWindow()
{
InitializeComponent();
fsw1 = new FileSystemWatcher()
{
Path = "Folder",
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.DirectoryName,
IncludeSubdirectories = false,
Filter = "",
EnableRaisingEvents = true
};
fsw1.Deleted += new FileSystemEventHandler(OnFileSystemItemDeleted);
fsw2 = new FileSystemWatcher()
{
Path = "Folder/Folder",
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.DirectoryName,
IncludeSubdirectories = false,
Filter = "",
EnableRaisingEvents = true
};
fsw2.Deleted += new FileSystemEventHandler(OnFileSystemItemDeleted);
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
//fsw1.EnableRaisingEvents = false;
//fsw2.EnableRaisingEvents = false;
System.IO.Directory.Delete("Folder", true);
}
void OnFileSystemItemDeleted(object sender, FileSystemEventArgs e)
{
}
FileSystemWatcher fsw1, fsw2;