gpt4 book ai didi

c# - FileSystemWatcher 事件未触发

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:59 25 4
gpt4 key购买 nike

我的 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);

关于c# - FileSystemWatcher 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23817735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com