c# - 文件系统观察器双条目

标签 c# .net filesystemwatcher

我制作了一个小型winforms应用程序来监视某个文件夹中是否有新的pdf文件,如果在特定文件夹中创建了新的pdf文件,它会将其复制到其他位置。

我遇到的问题是文件系统观察程序在我的列表框中创建了两个/多个条目,我该如何解决这个问题?

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

最佳答案

您需要跟踪已由 FileSystemWatcher 引发事件的文件(在集合或字典中)。根据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.

关于c# - 文件系统观察器双条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9902758/

相关文章:

c# - DateTime.Now 缺少 1 小时夏令时

c# - 用于重命名冲突默认值的 ProtoEnum 属性用法

c# - 如何在 WCF 中实现可靠的 UDP 传输?

c# - 即使 IncludeSubdirectories 设置为 false,FileSystemWatcher 也会获取子目录事件

c# - 网络机器上的 FileSystemWatcher 设置凭据

c# - ItemDataBound 未触发

c# - 关联引用未映射的类 (.net)

c# - 创建同一个 FileSystemWatcher 的多个实例

c# - 如何限制浏览器

c# - 在 C# 中使用 FileSystemWatcher 确定复制/写入进度