C#FileSystemWatcher.Deleted不是射击"正常"删除?正常、不是、FileSystemWatcher、Deleted

2023-09-03 06:24:28 作者:山水不相逢

code:

FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path, "*.exe");
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
fileSystemWatcher.EnableRaisingEvents = true;

在创建活动工作正常,但已删除的事件只发射,删除目录/或EXE是 SHIFT 的时候。但正常删除(移动到回收站的)不工作/触发事件!

The Created Event works fine, but the Deleted Event is only firing, when Deleting a Directory/or Exe with SHIFT. But normal-delete (moving to recycle bin) isn't working/firing the event!

如何解决这个问题呢?

推荐答案

这是预期的行为,因为该文件实际上并未删除。它的移动

This is expected behaviour as the file isn't actually deleted: it's moved.

尝试连接到

filesystemWatcher.Renamed

如果文件移动到回收站,而不是检查。

and checking if the file is moved to the Recycle Bin instead.

查找其中回收站实际上是在文件系统中是不平凡的,你要知道。有些code其他用户发布的(未经试验)就在这里:http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5d2be9aa-411c-4fd1-80f5-895f64aa672a/ - 和这里:How我可以告诉大家,一个目录在C#中的回收站?

Finding where the recycle bin actually is in the filesystem is not trivial, mind you. Some code posted by others (untried) is here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5d2be9aa-411c-4fd1-80f5-895f64aa672a/ - and also here: How can I tell that a directory is the recycle bin in C#?

 
精彩推荐