如何判断文件是否已重命名?
我正在更新一些旧代码,以更有效地使用 .ini
文件进行参数存储。基本上,一旦读取所有设置,它就会在内部缓存这些设置,以便可以在不打开文件的情况下再次读取它们。在使用缓存之前,我将缓存创建时间与文件的上次修改时间进行比较,并使用更新的文件重新创建缓存。这非常有效,除非用户重命名文件,因为该操作不会更新上次修改时间。
因此,如果我将 app.ini
复制到 app - copy.ini
,请在程序外部修改 app - copy.ini
,然后删除 < code>app.ini 并将副本重命名为 app.ini
,我的程序现在使用过时的缓存。即使我的程序捕获了 app.ini
的删除并清除了缓存,当副本被重命名时它也不会重建缓存。该程序设计为在无人值守的情况下运行很长时间,因此我想避免持续监视该文件 - 仅在需要读取参数时检查它。
I'm updating some legacy code to more efficiently use .ini
files for parameter storage. Basically, it caches all the settings internally once they're read so that they can be read again without opening the file. Before using the cache, I compare the cache creation time to the last modified time of the file, and recreate the cache with the updated file. This works great, except when users rename files, because that action doesn't update the last modified time.
So if I copy my app.ini
to app - copy.ini
, modify app - copy.ini
outside of my program, then delete app.ini
and rename the copy to app.ini
, my program is now using an outdated cache. Even if my program caught the deletion of app.ini
and cleared the cache, it would not rebuild the cache when the copy was renamed. The program is designed to run for a very long time unattended, so I would like to avoid continually monitoring the file - only check it once I need to read a parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是 vb6 的文件监视程序,您可以通过与 .net 互操作来尝试它或使用类似的东西 VB6实现
What you are needing is a filewatcher for vb6, you could try it by interop with .net or use something like this VB6 Implementation
对于这种情况,您可以保留程序正在使用的每个
.ini
文件内容的哈希值,并定期检查每个文件。如果哈希值与上次检查的值不同,则它的内容已更改,或者是此重命名方案导致的不同文件。For that scenario, you could keep a hash of the contents of each
.ini
file the program is using and periodically check each file. If the hash is different than what it was last time you checked, then it's contents were changed or it's a different file caused by this rename scenario.