我们有第三方应用程序将文件写入目录然后将其删除。 我们希望在删除文件之前复制该文件。
我们有:
FileSystemWatcher watcher;
private void WatchForFileDrop()
{
watcher = new FileSystemWatcher();
watcher.Path = "c:\\FileDrop";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
}
private void OnCreated(object source, FileSystemEventArgs e)
{
//Copy the file to the file drop location
System.IO.File.Copy(e.FullPath, "C:\\FileDropCopy\\" + e.Name);
}
FileSystemWatcher确实有效。它将看到该文件已创建并进入OnCreated()。该文件在目录中创建。
唯一的问题是文件为空,文件大小为0kb。
我想仔细检查一下我为什么文件为空的想法。是因为第三方应用程序如此快速地删除了文件,因为它没有机会进行正确的复制? 谢谢你看看。
答案 0 :(得分:2)
选项1: 您应该查看挂钩代码以删除事件,而不是查看FileSystemWatcher。 您可以在Stack Overflow上查看此评论:https://stackoverflow.com/a/4395147/442470
选项2: 一旦FileSystemWatcher意识到创建了文件,就更改文件的权限,使其无法删除。