0

(ネットワーク共有上の)フォルダーを監視し、ファイル名が変更されてファイルがサブディレクトリに移動されたときに通知を受ける必要があります(すべて1つのイベント)。このイベントはおそらく1日に2回発生します。同じことをFileSystemWatchers複数のフォルダで監視するために複数のフォルダがあります。

ただし、FileSystemWatcherイベントが欠落していることは悪名高いことであり、それを実現することはできません。環境を複製してみましたが、うまくいくようですが、特に何かをしているからなのかわかりません。

イベントのみを監視している場合OnRenamedでも、問題が発生する可能性がありますか、それともイベントを見逃さないようにすることができますか?

4

1 に答える 1

0

正しく機能しているネットワークでは、イベントの欠落に関連するエラーは発生しないはずですが、ネットワーク上の単純な一時的な中断により、FileSystemWatcherの監視が役に立たなくなることに注意する必要があります。

ネットワーク共有への接続が一時的に切断されると、FileSystemWatcherでエラーが発生し、接続が再確立されても、FileSystemWatcherは通知を受信しなくなります。

これは、MSDNにあるわずかに適合した例です。

static void Main()
{
    // Create a new FileSystemWatcher and set its properties.
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"\\yourserver\yourshare";
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    // Only watch text files.
    watcher.Filter = "*.txt";

    // Add handler for errors.
    watcher.Error += new ErrorEventHandler(OnError);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

    // Wait for the user to quit the program.
    Console.WriteLine("Press \'q\' to quit the sample.");
    while(Console.Read()!='q');
}
private static void OnError(object source, ErrorEventArgs e)
{
    //  Show that an error has been detected.
    Console.WriteLine("The FileSystemWatcher has detected an error");

    //  Give more information if the error is due to an internal buffer overflow.
    Type t = e.GetException().GetType();
    Console.WriteLine(("The file system watcher experienced an error of type: " + 
                       t.ToString() + " with message=" + e.GetException().Message));
}

このコンソールアプリケーションを起動してからネットワーク接続を無効にしようとすると、Win32Exceptionが表示されますが、もう一度実行すると、実行中のFileSystemWatcherによってエラーイベントが表示されなくなります。

于 2012-12-18T10:46:23.733 に答える