2011-10-10 18 views
1

我正在開發觀察文件更改,顯示通知並將更改列表插入listview.I很難只想更改通知如果更改excel文件的屬性。現在如果更改excel屬性,顯示創建,刪除,重命名通知。 任何人都可以向我建議嗎?如何看C#中的excel文件更改?

代碼:

string filepath = C:\New Folder; 
private void watcher_Changed(object sender, FileSystemEventArgs e) 
    { 
     sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1); 
     if (sfilepath == filepath) 
     { 
      FileInfo fileInfo = new FileInfo(e.FullPath); 
      DateTime lastWriteTime = fileInfo.LastWriteTime; 
      DateTime nowdt = DateTime.Now; 
      if (lastWriteTime == nowdt) 
      {      
       this.notifyIcon1.ShowBalloonTip(1, "File " + e.ChangeType, e.FullPath, ToolTipIcon.Info);           
      } 
     } 
    } 

    private void watcher_Created(object sender, FileSystemEventArgs e) 
    { 
     sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1); 
     if (sfilepath == filepath) 
     { 
      this.notifyIcon1.ShowBalloonTip(1, "File " + e.ChangeType, e.FullPath, ToolTipIcon.Info);         
     } 
    } 

    private void watcher_Deleted(object sender, FileSystemEventArgs e) 
    { 
     sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1); 
     if (sfilepath == filepath) 
     { 
      this.notifyIcon1.ShowBalloonTip(1, "File " + e.ChangeType, e.FullPath, ToolTipIcon.Info);        
     } 
    } 

    private void watcher_Renamed(object sender, RenamedEventArgs e) 
    { 
     sfilepath = e.FullPath.Substring(0, e.FullPath.Length - e.Name.Length - 1); 
     if (sfilepath == filepath) 
     { 
      this.notifyIcon1.ShowBalloonTip(1, "File Renamed", e.OldFullPath + " renamed to " + e.FullPath, ToolTipIcon.Info);         
     } 
    } 

更改列表:

New Microsoft Excel ワークシート.xls 2011/10/10 11:00:15 Created C:\New Folder 
B1F38000    2011/10/10 11:00:55 Created C:\New Folder 
New Microsoft Excel ワークシート.xls~RF83f213.TMP 2011/10/10 11:01:16 Created C:\New Folder 
New Microsoft Excel ワークシート.xls 2011/10/10 11:01:16 Deleted C:\New Folder 
New Microsoft Excel ワークシート.xls 2011/10/10 11:01:16 Renamed C:\New Folder 
New Microsoft Excel ワークシート.xls~RF83f213.TMP 2011/10/10 11:01:18 Deleted C:\New Folder 
+0

您可以檢查FileSystemEventArgs的ChangeType –

回答

0

您可以設置一個FileSystemWatcher看只是一個特定的文件夾,所以你對filepath檢查是多餘的。

否則,做的事情很像你現在,但檢查觸發事件,以確定它是否有XLSXLSXXLSM或任何文件上的其他Excel擴展的文件的文件名。

相關問題