2010-06-06 202 views
-1

我已經通過添加字符串屬性(fileToTest)擴展了標準FileSystemWatcher類,現在我還需要擴展FileSystemEventArgs以添加此屬性,我該怎麼做?將屬性添加到EventArgs

我延長FileSystemWatcher對象:

class AleFileSystemWatcher : FileSystemWatcher 
{ 
    public string fileToTest { get; set; } 
} 

的FileSystemEventArgs fileToTest屬性應該是相同AleFileSystemWatcher fileToTest的。

我可以這樣做嗎?

回答

1

個人而言,我不會擴展FileSystemWatcher,而是將它作爲類中的實例變量。你真的不延長FileSystemWatcher對象的功能,其主要方面,但要利用它的功能(即監聽改變/創建/任何文件,並匹配與您正在查找的文件)

public class SpecificFileWatcher 
{ 
    public string FileToTest { get; set; } 

    private readonly FileSystemWatcher iWatcher; 


    public class SpecificFileWatcher(FileSystemWatcher watcher) 
    { 
    iWatcher = watcher; 
    iWatcher.Changed += iWatcher_Changed; //whatever event you need here 
    } 

    //eventhandler for watcher 
    public ... 
    { 
    if(e.FileName == FileToTest) 
     Console.WriteLine("file found"); 
    } 
} 
+0

+1我完全同意 – digEmAll 2010-06-06 10:32:11