简体   繁体   English

当我实际删除文件时触发C#FileSystemWatcher,但是当我使用File.Move时不会触发

[英]C# FileSystemWatcher firing when i physically drop a file, however not when i use File.Move

Excel files are dropped manually into a local folder, there is a FileWatcher which converts the file into a new filestructure and moves it to the next folder which also have a filewatcher. 手动将Excel文件拖放到本地文件夹中,其中有一个FileWatcher,它将文件转换为新的文件结构,并将其移动到下一个具有FileWatcher的文件夹中。 The problem is that when this file is moved to the next folder the filewatcher does not fire any event. 问题在于,当此文件移动到下一个文件夹时,filewatcher不会触发任何事件。 However if i cut it and drop it physically the event fires. 但是,如果我将其切下并实际放下,则会触发该事件。 I am using File.Move to copy file from folder1 to folder2 我正在使用File.Move将文件从folder1复制到folder2

you should look at FileSystemWatcher detect when file is moved to folder 您应该查看FileSystemWatcher来检测文件何时移动到文件夹

Actually when there is a move, the filesystemwatcher send a delete (in the source directory watcher) and a create (in the target directory watcher). 实际上,如果有移动,文件系统监视程序将发送一个删除操作(在源目录监视程序中)和一个创建操作(在目标目录监视程序中)。

try to use renamed event. 尝试使用重命名的事件。

Another reason could be the buffer size may exceeded. 另一个原因可能是缓冲区大小可能超出。

 Public void WatchItBaby()
 {
    // ...
    FileSystemWatcher watcher = new FileSystemWatcher(@"c:\temp\", "*.txt");            
    watcher.Created += new FileSystemEventHandler(OnChangedOrRenamed);          
    watcher.Renamed += new RenamedEventHandler(OnChangedOrRenamed);
    watcher.EnableRaisingEvents = true;
    // ...
 }

 private void OnChangedOrRenamed(object source, FileSystemEventArgs e)
 {
    // stuff        
 }

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

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