如何检查文件锁? [英] How to check for file lock?

查看:153
本文介绍了如何检查文件锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来检查,而无需使用try / catch块的文件是否被锁定?

Is there any way to check whether a file is locked without using a try/catch block?

现在,我知道的唯一途径是刚刚打开该文件并捕获任何 System.IO.IOException

Right now, the only way I know of is to just open the file and catch any System.IO.IOException.

推荐答案

没有,遗憾的是,如果你仔细想想,这些信息将变得毫无价值无论如何,因为该文件可能被锁定在第二天第二(阅读:短的时间跨度)

No, unfortunately, and if you think about it, that information would be worthless anyway since the file could become locked the very next second (read: short timespan).

为什么要特别指出的是,你需要知道,如果该文件被锁定呢?明知可能给我们提供好的建议一些其他的方式。

Why specifically do you need to know if the file is locked anyway? Knowing that might give us some other way of giving you good advice.

如果您的code是这样的:

If your code would look like this:

if not locked then
    open and update file

随后两线之间,另一个进程可以很容易地锁定文件,让你同样的问题,你试图避免首先:例外

Then between the two lines, another process could easily lock the file, giving you the same problem you were trying to avoid to begin with: exceptions.

这篇关于如何检查文件锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆