2016-02-10 18 views
0

我很抱歉,如果這是一個非常簡單的修復,因爲我是C#的新手。 下面是在Windows監視文件夾中的代碼片段:C#.net,調用方法時如何傳入變量的語法方法

FileSystemWatcher watcher = new FileSystemWatcher(); 
      watcher.Path = label1.Text; 

      watcher.NotifyFilter = NotifyFilters.LastWrite | 
            NotifyFilters.LastAccess | 
            NotifyFilters.FileName | 
            NotifyFilters.DirectoryName; 
" 
      watch.Filter = "*.*"; 

      watcher.Created += new FileSystemEventHandler(OnChanged(label4)); 
      watcher.EnableRaisingEvents = true; 

正如你所看到的,而我在監視文件夾中的文件被「創建」,爲每個文件將執行的方法OnChanged

我的.net應用程序有一個標籤,我想將標籤文本作爲字符串變量傳遞給OnChanged方法。 我的問題是如何將一個變量傳入OnChanged方法? 它出現了我嘗試過的任何語法,包括上面的語法Visual Studio 2010不喜歡它。

的OnChanged方法是這樣的:

  public static void OnChanged(object source, FileSystemEventArgs e, String label4) 
      { 
       FileInfo file = new FileInfo(e.FullPath); 
       String fileName = file.Name; 
       String outputPath = label4.Text + file.Name; 
      } 
+1

https://stackoverflow.com/questions/8644253/pass-parameter-to-eventhandler – SiD

回答

1

您不能直接通過你的觀點,但你可以使用lambda表達式來包裝它。

watcher.Created += (source, e) => OnChanged(source, e, label4);