2011-07-08 60 views
1

這是我的第一個C#文章。綁定事件在另一個類

我對事件綁定有疑問。

我有一個FileWatcher,我想綁定到一個名爲FileWatcherEvents的單獨類中定義的函數。

我不希望事件在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

你確定事件甚至被解僱嗎?你有沒有得到這個與Program類的事件處理程序一起工作? – BlueMonkMN

相關問題