简体   繁体   English

FileSystemWatcher - 文件复制期间的3个事件

[英]FileSystemWatcher - 3 events during File copy

Is that, every time FileSystemWatcher will generate 3 events when a file is copied to the FileWatcher folder 就是这样,每次FileSystemWatcher将文件复制到FileWatcher文件夹时都会生成3个事件

Example : Am getting Created - > Changed -> Changed (during new file) Or Changed - > Changed - > Changed (during overwrite of existing file) event , when file is copied to the FileWatcher folder using File.Copy(source,watcherFolder,true). 示例:使用File.Copy(source,watcherFolder,将文件复制到FileWatcher文件夹)时, 创建 - >已更改 - >已更改 (在新文件期间)或已更改 - >已更改 - >已更改 (在覆盖现有文件期间)事件真正)。

Am getting 3 events when copying file of different size (1kb , 67kb, 100MB, 500MB, 1 GB files). 复制不同大小的文件(1kb,67kb,100MB,500MB,1GB文件)时会收到3个事件。 i have registerd for Created and Changed event in FileSystemWatcher 我已经在FileSystemWatcher中注册了Created和Changed事件

If your question is about the multiple events you are receiving, this is a normal behavior. 如果您的问题是关于您收到的多个事件,这是正常行为。 File copy can raise multiple event. 文件复制可以引发多个事件。

You can check the changes in FileSystemEventArgs.ChangeType and ignore some of the events. 您可以检查FileSystemEventArgs.ChangeType中的更改并忽略某些事件。 Here is the WatcherChangeTypes Enumeration . 这是WatcherChangeTypes枚举

Take a look at FileSystemWatcher Remarks. 看一下FileSystemWatcher备注。

About the Created event, it will be raised in the destination folder. 关于Created事件,它将在目标文件夹中引发。

For example, you create two instances of FileSystemWatcher. 例如,您创建两个FileSystemWatcher实例。 FileSystemWatcher1 is set to watch "C:\\My Documents", and FileSystemWatcher2 is set to watch "C:\\Your Documents". FileSystemWatcher1设置为观看“C:\\ My Documents”,FileSystemWatcher2设置为观看“C:\\ Your Documents”。 If you copy a file from "My Documents" into "Your Documents", a Created event will be raised by FileSystemWatcher2, but no event is raised for FileSystemWatcher1. 如果将文件从“我的文档”复制到“您的文档”中,FileSystemWatcher2将引发Created事件,但不会为FileSystemWatcher1引发任何事件。 Unlike copying, moving a file or directory would raise two events. 与复制不同,移动文件或目录会引发两个事件。 From the previous example, if you moved a file from "My Documents" to "Your Documents", a Created event would be raised by FileSystemWatcher2 and a Deleted event would be raised by FileSystemWatcher1. 在上一个示例中,如果您将文件从“我的文档”移动到“您的文档”,则FileSystemWatcher2将引发Created事件,FileSystemWatcher1将引发Deleted事件。

As mentioned in the comments, you should look for a file being created and then attempt a file lock to determine if it is fully copied. 如评论中所述,您应该查找正在创建的文件,然后尝试文件锁定以确定它是否已完全复制。

    while ($true)
    {
        try{ 
            [IO.file]::openwrite("<insert file path>").close()
            break
        }
        catch { start-sleep -Seconds 60 }
    }

If you are in control of the file copy operation, a simple thing to do is to create the file with a temporary name and rename it once it has been copied. 如果您控制文件复制操作,一件简单的事情就是使用临时名称创建文件,并在复制后重命名该文件。 Let the FileSystemWatcher watch for the Rename event. FileSystemWatcher监视Rename事件。 This event is fired only once. 此事件仅触发一次。

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

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