繁体   English   中英

FileSystemWatcher事件未触发

[英]FileSystemWatcher Events Not Firing

我的FileSystemWatcher没有引发任何事件。 我看过这些类似的问题,似乎没有一个答案可以解决我的问题:

*编辑:我的目标是捕获XLS文件何时复制到目录或在目录中创建。

监控类:

public class Monitor
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    readonly string bookedPath = @"\\SomeServer\SomeFolder\";

    public delegate void FileDroppedEvent(string FullPath);
    public event FileDroppedEvent FileDropped;

    public delegate void ErrorEvent(Exception ex);
    public event ErrorEvent Error;

    public Monitor()
    {
        watcher.Path = bookedPath;
        watcher.Filter = "*.xls";
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        watcher.Error += new ErrorEventHandler(watcher_Error);
    }

    void watcher_Error(object sender, ErrorEventArgs e)
    {
        Error(e.GetException());
    }

    void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        if (e.ChangeType != WatcherChangeTypes.Created) return;
        FileDropped(e.FullPath);
    }

    public void Start()
    {
        watcher.EnableRaisingEvents = true;
    }

    public void Stop()
    {
        watcher.EnableRaisingEvents = false;
    }
}

使用列表框的简单形式:

public partial class Form1 : Form
{
    Monitor monitor = new Monitor();

    public Form1()
    {
        InitializeComponent();
        FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        Load += new EventHandler(Form1_Load);
        monitor.FileDropped += new Monitor.FileDroppedEvent(monitor_FileDropped);
        monitor.Error += new Monitor.ErrorEvent(monitor_Error);
    }

    void Form1_Load(object sender, EventArgs e)
    {
        monitor.Start();
    }

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        monitor.Stop();
    }

    void monitor_Error(Exception ex)
    {
        listBox1.Items.Add(ex.Message);
    }

    void monitor_FileDropped(string FullPath)
    {
        listBox1.Items.Add(FullPath);
    }
}

我究竟做错了什么?

试试看 为我完成了非常相似的任务。

watcher.NotifyFilter = NotifyFilters.FileName;   
watcher.Created += new FileSystemEventHandler(handler);     
watcher.Renamed += new RenamedEventHandler(handler);

这可能是因为文件元数据尚未更新。 如果您连续写入文件,可能会发生这种情况。

您是否尝试过以下方法:

watcher.Path = directory name;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.xls";

watcher.Changed += OnDirectoryChange;
watcher.Error += OnError;
watcher.EnableRaisingEvents = true;

// Watch only files not subdirectories.
watcher.IncludeSubdirectories = false;

我认为您的问题与过滤器和事件有关。 NotifyFilters.LastAccess仅在打开文件时触发。 尝试使用:

NotifyFilters.LastWrite | NotifyFilters.CreationTime

这将监视写入/创建的文件。 接下来,连接到Created委托以处理新创建的文件:

watcher.Created += YourDelegateToHandleCreatedFiles

FileSystemWatcher工作方式是首先使用NotifyFilters来限制事件触发器。 然后,您使用实际事件进行工作。 通过钩入Created事件,您将仅在Created文件时起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM