当 Excel 2007 以兼容模式打开 XLS 文件时,FileSystemWatcher.Changed 立即触发

发布于 2024-08-24 21:02:30 字数 591 浏览 4 评论 0原文

我们使用 FileSystemWatcher 来监视从文档管理系统打开的文档,如果用户保存文档,我们会询问他们是否也希望在我们的系统中更新它们。

我们在 Excel 2007 中遇到 XLS 文件问题(尚未验证该问题在 2003 中不存在,但似乎只是在 2007 中以兼容模式打开的文件),其中打开文件后立即触发 Changed 事件,并且然后在关闭文件时再次执行此操作,即使没有任何更改或用户选择在关闭时不保存。打开 XLSX 文件时不存在相同的行为。

我编写了一个测试应用程序来验证该行为,您可以在 (http://www. just2guys.net/SOFiles/FSWExcel.zip)。在应用程序中,每种 NotifyFilter 类型都有一个 FileSystemWatcher,因此触发 Changed 事件的原因一目了然。

您能想到什么方法只在用户实际以某种方式保存文档时提示用户?我可以在调用 Process.Start 后开始监视文件,这允许我在打开文档时跳过该消息,但在关闭文档时我仍然会收到一条消息,即使没有任何更改。

We use a FileSystemWatcher to monitor documents opened from our Document Management system, and if the user saves the document, we ask if they would also like them updated in our system.

We have a problem with XLS files in Excel 2007 (have not verified that the problem does not exist in 2003, but it only seems to be files that open in compatibility mode in 2007) where the Changed event fires immediately upon opening the file, and then once more upon closing the file, even if nothing has changed or the user chooses not to save upon closing. This same behavior does not exist when opening XLSX files.

I wrote a test app to verify the behavior, which you can find at (http://www.just2guys.net/SOFiles/FSWExcel.zip). In the app, there is one FileSystemWatcher for each NotifyFilter type, so that it is apparent why the Changed event was fired.

Any way you can think of to only prompt the user when the document is actually saved in some way by the user? I can start monitoring the file after Process.Start is called, which allows me to skip the message upon opening the document, but I still get one upon closing the document, even when nothing was changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画骨成沙 2024-08-31 21:02:30

我在 Word 中注意到了这一点,您可以简单地重现它:

如果您使用 Office 应用程序打开 .doc 或 .xls 文件,时间戳将更改为文档打开的时间。

如果您关闭应用程序,时间戳将重置为其原始值(仅当您未保存时)。

因此,对于 Office 文件,您需要两项额外的检查:

  • 在打开时,当时间戳设置为当前时间时,检查该文件是否可以以独占方式打开

  • 关闭时,检查时间戳是否“接近”当前日期和时间,或检查文件的存档标志

I noticed this with Word, and you can simply reproduce it:

If you open a .doc or an .xls file with an Office application, the timestamp changes to the time the document was opened.

If you close the application, the timestamp is reset to its original value (only if you did ont save).

So for Office files, you need two additional checks:

  • on Open, when timestamp is set to current time, check that the file can be opened exclusively

  • on Close, check whether the timestamp is "close to" the current date and time, or check the file's Archive flag

浪推晚风 2024-08-31 21:02:30

文件系统观察者通常是邪恶的。它烧伤了很多人,因为它在文件完全写入之前就注册了事件。

我们已经能够通过删除它来代替睡眠循环模式、在采取操作之前添加静态延迟 (Thread.Sleep) 或两者的组合(接收事件、生成线程和循环)来解决这个问题。在线程中,直到您认为没有任何内容通过检查最后的修改日期/时间来更新它)。

File system watcher is usually evil. It burns a lot of people because it registers events before the file is completely written.

We have been able to get around it by either removing it in lieu of a sleep loop pattern, adding a static delay (Thread.Sleep) before taking action, or a combo of the two (receiving the event, spawning a thread, and looping in the thread until you think that nothing is updating it by checking the last mod date/time).

国粹 2024-08-31 21:02:30

Excel 每次打开文件时都会创建一个临时文件 - 这可能是您看到的事件。检查 FileSystemEventArgs 的 Name 属性,如果文件名以“~”开头,则忽略该事件。

Excel creates a temporary file each time it opens a file - this is probably the event you are seeing. Check the Name property of FileSystemEventArgs and ignore the event if the filename starts with '~'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文