在 javascript 中使用 scripting.filesystemobject 并检查锁定的文件
我有一些代码使用 activex 文件系统对象从 Javascript 中的 ini 文件中读取。
这不是特别有效,但可以完成工作,将整个文件读入数组,附加任何更改并写回。
我遇到的问题是另一个进程,一个 C# XBAP 应用程序正在从此 ini 文件中读取(使用 getprivateprofilestring),同时我可能会尝试在 JS 中写入它。
当文件被锁定或部分文件被锁定时,JavaScript 会失败,并且文件最终会被损坏甚至完全清除 - 因为我每次都试图写回整个文件。
最好是,我需要一种方法来确定文件是否在 JavaScript 中被锁定,因为写入并不紧急,我想让任何读取先完成。
只是似乎无法找到同步这两种完全独立的访问文件的方式。
I have some code that reads from an ini file in Javascript, using activex filesystem objects.
This isn't particularly efficient but does the job, reading the whole file into an array, appending any changes and writing back.
The problem i'm having is that another process, a C# XBAP application is reading from this ini file (using getprivateprofilestring) at the same time as I could potentially be trying to write to it in the JS.
The javascript fails as the file is locked, or part of it, and the file ends up getting corrupted or even totally cleared - as I am trying to write back the whole file each time.
Preferably, what I need is a way to determine if a file is locked in javascript, as the writes are not urgent and I want to let any reads finish first.
Just can't seem to find anyway of syncing these two completely seperate ways of accessing the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你可以使用
try/catch
。如果打开文件进行追加 (OpenTextFile([filename],8
)),它应该引发异常。写入/保存文件也应该如此(如果文件被锁定,try
会引发异常)。May be you could use
try/catch
. If you open the file for appending (OpenTextFile([filename],8
)) it should raise an exception. Same should be true for writing/saving the file (if the file is lockedtry
raises an exception).