在 javascript 中使用 scripting.filesystemobject 并检查锁定的文件

发布于 2024-10-05 03:40:11 字数 373 浏览 0 评论 0原文

我有一些代码使用 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 技术交流群。

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

发布评论

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

评论(1

白云不回头 2024-10-12 03:40:11

也许你可以使用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 locked try raises an exception).

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