2012-03-28 52 views
2

我做了一個小的winforms應用程序來監視某個文件夾中的新PDF文件,如果在particulair文件夾中創建了一個新的pdf文件,它將它複製到另一個位置。Filesystemwatcher double條目

我遇到的問題是filesystemwatcher在我的列表框中創建雙/多個條目,我該如何解決?

namespace Scanmonitor 
{ 
    public partial class Form1 : Form 
    { 
     FileSystemWatcher watcher = new FileSystemWatcher(); 
     DateTime lastRead = DateTime.MinValue; 


     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      FileWatch(); 
     } 

     public void FileWatch() 
     { 
      watcher.Path = @"C:\Scanner"; 
      watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite; 
      watcher.Filter = "*.pdf"; 
      watcher.Changed += new FileSystemEventHandler(OnChanged); 
      watcher.EnableRaisingEvents = true; 
     } 

     public void OnChanged(object source, FileSystemEventArgs e) 
     { 
      scannerListBox.Items.Add(e.FullPath); 
      scannerListBox.SelectedIndex = scannerListBox.Items.Count - 1; 
      FileMove(scannerListBox.SelectedItem.ToString()); 
     } 

     public void FileMove(string filePath) 
     { 
      try 
      { 
       System.IO.File.Copy(filePath, @"\\share\Data\Scans op OCE 600\" + Path.GetFileName(filePath)); 
      } 
      catch (Exception ex) 
      { 
       ToolLabel.Text = ex.Message.ToString(); 
      } 
     } 
    } 
} 

}

得到它的工作。

public void OnChanged(object source, FileSystemEventArgs e) 
    { 
     try 
     { 
      watcher.EnableRaisingEvents = false; 
      FileInfo objFileInfo = new FileInfo(e.FullPath); 
      if (!objFileInfo.Exists) return; 
      System.Threading.Thread.Sleep(5000); 

      FileInfo fileinformatie = new FileInfo(e.FullPath); 
      string strCreateTime = fileinformatie.CreationTime.ToString(); 
      string strCreateDate = fileinformatie.CreationTime.ToString(); 

      strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" ")); 
      strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" ")); 

      ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate); 
     } 
     catch (Exception ex) 
     { 
      ToolLabel.Text = ex.Message.ToString(); 
     } 
     finally 
     { 
      watcher.EnableRaisingEvents = true; 
     } 
    } 
+0

的可能重複的[C++ WINAPI:ReadDirectoryChangesW()接收雙通知(http://stackoverflow.com/questions/14036449/c-winapi-readdirectorychangesw-receiving-double-notifications) – 2012-12-27 02:55:34

回答

0
public void OnChanged(object source, FileSystemEventArgs e) 
{ 
    try 
    { 
     watcher.EnableRaisingEvents = false; 
     FileInfo objFileInfo = new FileInfo(e.FullPath); 
     if (!objFileInfo.Exists) return; 
     System.Threading.Thread.Sleep(5000); 

     FileInfo fileinformatie = new FileInfo(e.FullPath); 
     string strCreateTime = fileinformatie.CreationTime.ToString(); 
     string strCreateDate = fileinformatie.CreationTime.ToString(); 

     //Ignore this, only for my file information. 
     strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" ")); 
     strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" ")); 

     ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate); 
    } 
    catch (Exception ex) 
    { 
     ToolLabel.Text = ex.Message.ToString(); 
    } 
    finally 
    { 
     watcher.EnableRaisingEvents = true; 
    } 
} 
2

您需要跟蹤已由FileSystemWatcher引發事件的文件(在集合或字典中)。根據MSDN

通用文件系統操作可能引發多個事件。例如,當文件從一個目錄移動到另一個目錄時,可能會引發幾個OnChanged和一些OnCreated和OnDeleted事件。移動文件是一項複雜的操作,由多個簡單操作組成,因此引發多個事件。同樣,某些應用程序(例如防病毒軟件)可能會導致由FileSystemWatcher檢測到的其他文件系統事件。