C# 检测文件在不同的文件夹中 move

标签 c# move filesystemwatcher

我想检测特定文件夹中的每个文件更改(数据更改除外)。我决定使用 System.IO.FileSystemWatcher 来管理它。

// 
// fileSysWatchFile
// 
this.fileSysWatchFile.EnableRaisingEvents = true;
this.fileSysWatchFile.IncludeSubdirectories = true;
this.fileSysWatchFile.NotifyFilter = System.IO.NotifyFilters.FileName;
this.fileSysWatchFile.SynchronizingObject = this;
this.fileSysWatchFile.Created += new System.IO.FileSystemEventHandler(this.fileSysWatchFile_Created);
this.fileSysWatchFile.Deleted += new System.IO.FileSystemEventHandler(this.fileSysWatchFile_Deleted);
this.fileSysWatchFile.Renamed += new System.IO.RenamedEventHandler(this.fileSysWatchFile_Renamed);

就目前而言...检测到新文件。检测到文件删除。检测到文件重命名。

当我将文件 move 到子文件夹时,它首先检测到文件已删除,然后是新文件创建。

除了路径之外,我希望 move 与重命名相同。好像不是。我可以通过保存方式检测文件 move 吗?

顺便说一句...我只想检测文件更改而不是目录更改。

编辑:

附加信息为什么我必须检测 move 并且不能忍受删除,创建:

我想在另一个驱动器上重播相同的更改。如果我先删除,我会删除影子文件。然后我得到创建文件事件,原始文件已经丢失:-(。

所以我有一个驱动器 A,它是被监视的驱动器...... 和一个驱动器 B,其中包含具有相同文件名的文件。

除数据更改外的所有文件更改都应在驱动器 B 上重播。

最佳答案

文件删除/文件创建功能是文件 move 背后的功能。如果您只是将文件从一个文件夹 move 到另一个文件夹,这类似于重命名,但是如果将文件从一个磁盘 move 到另一个磁盘,或者在机器之间 move 文件呢?

而且,如果我正在查看指定的文件夹,只要该文件不存在,它也可能已被删除:)

如果您确定要捕获文件 move 的“事件”(从监视的文件夹到监视的子文件夹),我会维护一个最近删除文件的列表,并在每个文件创建事件时检查文件是否它是那个列表,表示事实上的文件 move 。

关于C# 检测文件在不同的文件夹中 move ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4290857/

相关文章:

c++ - 最佳C++ move 构造函数实现实践

java - OSGi noClassDefFound for java.nio.files.FileSystems$DefaultFileSystemHolder

c# - 如何使用 C# 监视进程的 IO 事件?

.net - System.IO.FileSystemWatcher 监视网络服务器文件夹 - 性能注意事项

C# HttpWebRequest Post 方法表单中的许多参数

c# - 从 Azure 中的 SOAP Web 服务检索列表时出现 TargetInitationException

c# - Entity Framework : A referential integrity constraint violation on many to many relationship

c# - 基于对象参数的模拟方法返回

powershell - move 带有文件的文件夹(异常(exception))

linux - 如何 move 和编号文件?