Filesystemwatcher double entries

2019-04-14 04:22发布

问题:

I made a small winforms application to monitor a certain folder for new pdf files, if a new pdf file is created in the particulair folder it will copy it to an other location.

The problem i'm having is that the filesystemwatcher creates double/multiple entries in my listbox, how can i solve this?

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();
            }
        }
    }
}

}

Got it working.

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;
        }
    }

回答1:

You need to keep track of files (in a collection or dictionary) that already raised an event by the FileSystemWatcher. According to MSDN

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.



回答2:

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;
    }
}