2012-05-31 21 views
2

我已經編寫了一個實用程序來確定何時更新Excel文件。一旦更新完成,我必須讀取相同的Excel文件。但是,我沒有通知有關更新,而是通知臨時文件創建(這對我沒有用處)。我怎樣才能在C#Windows窗體中執行此操作?LastWrite在FileSystemWatcher中的NotifyFilter

這裏使用的代碼片段:

    watcher.EnableRaisingEvents = true; 
        watcher.Filter = "*.xlsx"; 
        watcher.NotifyFilter = NotifyFilters.LastWrite; 
        watcher.Path = "G:\\Prerequisites Folder"; 
        watcher.SynchronizingObject = this; 
        watcher.Changed += new FileSystemEventHandler(watcher_Changed); 

      void watcher_Changed(object sender, FileSystemEventArgs e) 
      { 

      if (e.Name.StartsWith("~") == false) 
        btnRefreshPrequisites_Click(null, null); 
      } 

什麼,我很想念?

回答

0
void watcher_Changed(object sender, FileSystemEventArgs e) 
     { 
     //no need to check if (e.Name.StartsWith("~") == false) 
     // comment the if statement 
     if (e.Name.StartsWith("~") == false) 
       btnRefreshPrequisites_Click(null, null); 

     //do your work 
     } 
3

這是通過設計,許多程序的通用方法。修改文件時,他們會嘗試確保這些修改不會因任何原因而破壞原始文件。因此,他們將原始文件複製到臨時名稱,對該臨時文件進行更改。當沒有出錯時,重命名原始文件,重命名臨時文件,使其與原始文件名稱相同,最後刪除重命名的原件。

因此,您還需要對重命名事件感興趣。