gpt4 book ai didi

c# - 使用 FileSystemWatcher 监视文件创建并在删除前复制它

转载 作者:行者123 更新时间:2023-11-30 16:57:55 24 4
gpt4 key购买 nike

我们有一个第 3 方应用程序,可以将文件写入目录,然后将其删除。我们想在删除该文件之前复制该文件。

我们有这个:

    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。

我想仔细检查我对文件为何为空的想法。是因为文件被第 3 方应用程序删除得太快以至于没有机会进行正确的复制吗?感谢您观看。

最佳答案

选项 1:与其查看 FileSystemWatcher,不如查看 Hook 代码以删除事件。您可以在 Stack Overflow 上查看此评论:https://stackoverflow.com/a/4395147/442470

选项 2:一旦您的 FileSystemWatcher 意识到文件已创建,就更改文件的权限,使其无法删除。

关于c# - 使用 FileSystemWatcher 监视文件创建并在删除前复制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25940768/

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