c#FileSystemWatcher在修改文件时不会引发事件。它仅在创建或删除文件时引发事件 [英] c# FileSystemWatcher does not raise an event when a file is modified. It only raises the event when a file is created or deleted

查看:67
本文介绍了c#FileSystemWatcher在修改文件时不会引发事件。它仅在创建或删除文件时引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用c#FileSystemWatcher时出现问题。修改文件时,它不会引发Changed事件。它仅在创建文件
或删除时引发事件。

注意:我在Windows 7上测试没问题,但是窗口10没有。

这是我的代码:

推荐答案

为了清楚起见,这完全有可能。它完全取决于文件的保存方式。例如,记事本每次都会重新创建文件。所以你得到一个删除后跟一个创建。但是,较大的文件往往需要多次写入,因此
您可能还会获得一个创建,然后进行多次更改。它完全取决于编写程序如何保存它。你必须处理它们。要观察文件的更改,您应该监视LastWrite和Size。监视FileName和DirectoryName
监视重命名,删除和新文件/文件夹。 LastAccess不执行任何操作,因为不再在文件系统中跟踪(至少默认情况下)。

Just to be clear, this is entirely possible. It completely depends upon how the file is saved. As an example, notepad used to recreate the file each time. So you'd get a delete followed by a create. However larger files tend to require multiple writes so you may also get a create followed by several changes. It is completely dependent on how the writing program saves it. You have to handle them all. For watching for changes to a file you should be monitoring LastWrite and Size. Monitoring FileName and DirectoryName watches for renames, deletes and new files/folders. LastAccess does nothing as that isn't tracked in the file system anymore (at least by default).

Michael Taylor

http://www.michaeltaylorp3。净

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于c#FileSystemWatcher在修改文件时不会引发事件。它仅在创建或删除文件时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆