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

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

问题描述

有没有什么方法可以在不使用 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.

如果您的代码如下所示:

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天全站免登陆