2011-07-08 8 views
1

これは私の最初のC#の投稿です。バインディングイベント別のクラスでは

イベントバインディングに関する質問があります。

FileWatcherEventsという別のクラスで定義されている関数にバインドしたいのは、FileWatcherです。

イベントをProgramクラスで宣言したくない場合、どうすればいいですか?

ご覧のとおり、私はCreatedDeletedのイベントをバインドしようとしています。

問題は、フォルダ内のファイルを削除または作成するときにこれらのイベントが呼び出されないことです。しかし、私がProgramクラスのイベントハンドラを宣言すると、動作します。

ご意見や洞察力があれば幸いです。

プログラム


using System.IO; 

class Program : ServiceBase 
{ 
    private FileSystemWatcher _watcher; 

    public Program() 
    { 
     FileWatcherEvents fwe = new FileWatcherEvents(); 
     this._watcher = new FileSystemWatcher(); 
     ((System.ComponentModel.ISupportInitialize)(this._watcher)).BeginInit(); 
     // 
     // _watcher 
     // 
     this._watcher.EnableRaisingEvents = true; 
     this._watcher.Filter = "*.txt"; 
     this._watcher.NotifyFilter = 
     ((NotifyFilters)(((((NotifyFilters.FileName 
      | NotifyFilters.DirectoryName) 
      | NotifyFilters.LastWrite) 
      | NotifyFilters.LastAccess) 
      | NotifyFilters.CreationTime))); 
     this._watcher.Path = "T:\\out\\"; 
     this._watcher.Deleted += new FileSystemEventHandler(fwe.ShipmentFileCreated); 
     this._watcher.Created += new FileSystemEventHandler(fwe.FileDeleted); 
     ((System.ComponentModel.ISupportInitialize)(this._watcher)).EndInit(); 
    } 

    static void Main(string[] args) 
    { 
     Program prg = new Program(); 
     Console.Write(FileManager.getNewestFile("T:\\out\\")); 
     while (Console.Read() != 'q') ; 
    } 
} 

FileWatcherEvents


class FileWatcherEvents 
{ 
    public void ShipmentFileCreated(object sender, FileSystemEventArgs e) 
    { 
     Console.WriteLine("CREATED: " + sender.ToString() + e.ToString()); 
    } 

    public void FileDeleted(object sender, FileSystemEventArgs e) 
    { 
     Console.WriteLine("DELETED: " + sender.ToString() + e.ToString()); 
    } 
} 

答えて

0

コードが正常に動作していることが判明し、イベントは発生しますが、機能があるため、民間FileSystemWatcherオブジェクトで*.txtフィルタではありませんでした。

1

私はあなたがプログラムレベルではなく、プログラムのコンストラクタの内部のような、より大きな範囲でFWEを宣言する必要があるだろうと信じています。さもなければ、オブジェクトは消え去り、場合によってはそれにつながるすべてのイベントもクリアされます(インスタンスがなくなるとインスタンス上のイベントを処理する関数に何が起こるのかは完全には分かりませんが、イベントはまだ発生しますが、彼らはもはや実行されません)。

編集: マイナーチェンジで作業するコードがあります。主に、EnableRaisingEventsをブロックの最後に移動する必要がありました。なぜなら、パスを設定する前に.NETが例外をスローするからです。その例外はどうして見えなかったのですか?

class Program 
{ 
    private FileSystemWatcher _watcher; 

    public Program() 
    { 
     FileWatcherEvents fwe = new FileWatcherEvents(); 

     this._watcher = new System.IO.FileSystemWatcher(); 
     this._watcher.Filter = "*.txt"; 
     this._watcher.NotifyFilter = ((System.IO.NotifyFilters)(((((
      System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName) 
      | System.IO.NotifyFilters.LastWrite) | System.IO.NotifyFilters.LastAccess) 
      | System.IO.NotifyFilters.CreationTime))); 
     this._watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
     this._watcher.Deleted += new System.IO.FileSystemEventHandler(fwe.ShipmentFileCreated); 
     this._watcher.Created += new System.IO.FileSystemEventHandler(fwe.FileDeleted); 
     this._watcher.EnableRaisingEvents = true; 
     Console.ReadLine(); 
    } 

    public static void Main() 
    { 
     Program prg = new Program(); 
     using (System.IO.StreamWriter w = new System.IO.StreamWriter(
      System.IO.Path.Combine(
      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TestFile.txt"), false)) 
     { 
      w.WriteLine(DateTime.Now.ToLongTimeString()); 
     } 
     Console.ReadLine(); 
    } 
} 

class FileWatcherEvents 
{ 
    public void ShipmentFileCreated(object sender, FileSystemEventArgs e) 
    { 
     Console.WriteLine("CREATED: " + sender.ToString() + e.ToString()); 
    } 

    public void FileDeleted(object sender, FileSystemEventArgs e) 
    { 
     Console.WriteLine("DELETED: " + sender.ToString() + e.ToString()); 
    } 
} 
+0

こんにちは、面白い考え、私はそれを試してみましょう、 – Triztian

+0

いいえ、それは動作しませんでした、それは 'プログラム'のメンバーと宣言し、まだコンソールにログなし – Triztian

+0

イベントが起動されても確実ですか?プログラムクラスのイベントハンドラで動作するようにしましたか? – BlueMonkMN

関連する問題