正在使用的 VBA 监视文件
我正在寻找一些东西(Win API 调用或其他)来在文件可供编辑(即不再使用)时通知我。我应该设置一个计时器来按一定时间间隔检查文件还是有一个好方法对文件设置监视?
I"m looking for something (Win API calls or whatever) to notify me when a file becomes available for editing (i.e. no longer in use). Should I set up a timer to check the files on some interval or is there a good way to set up a watch on the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FileSystemWatcher 没有帮助,Win32 FindFirstChangeNotification 也没有帮助:它们不会告诉您何时有人释放文件句柄。
最好的方法是定期尝试使用所需的访问权限打开文件,处理任何错误。
即使您收到文件可用的通知,也不能保证当您随后尝试打开该文件时该文件仍然可用。
FileSystemWatcher doesn't help, nor does the Win32 FindFirstChangeNotification: they won't tell you when someone releases a file handle.
Your best way is to periodically attempt to open the file with the access you want, handling any errors.
Even if you were notified that a file was available, that won't guarantee that it will still be available when you subsequently try to open it.
我不知道 .NET 的 System.IO.FileSystemWatcher 是做什么的,但如果您可以使用它,则可以在文件发生变化时获取事件。
I don't know what .NET's System.IO.FileSystemWatcher does, but if you could use that, you could get events when things about a file changes.