检查文件是否在使用中没有尝试捕捉?文件

由网友(Cripple丶伤了怎麼伪装)分享简介:有没有一种方法可以检查文件是否在使用或通过其他方法不只是想打开它,并捕获异常没有被打开?有没有服务的方法来测试这种事?Is there a way I can check if a file is in use or is not opened by other process without just trying...

有没有一种方法可以检查文件是否在使用或通过其他方法不只是想打开它,并捕获异常没有被打开?有没有服务的方法来测试这种事?

Is there a way I can check if a file is in use or is not opened by other process without just trying to open it and catching an exception? Is there no service method to test such a thing?

推荐答案

即使有,也不会做你多好,因为你仍然要以处理争用情况的文件成为捕获异常在初步核实,核实您的实际尝试打开/访问它的不可用。

Even if there was, it wouldn't do you much good since you would still have to catch the exception in order to handle the race condition where the file became unavailable in between your initial check and your actual attempt to open/access it.

我想不出任何令人信服的优势,为preliminary防守检查。它只是导致不必要的code复制。

I can't think of any compelling advantage to a preliminary defensive check. It just results in unnecessary code duplication.

如果没有的是的这样一个 IsFileAccessible 的功能,它很可能被实现为试图打开该文件一个巨大的try / catch块,陷入失败,并返回结果。

If there were such a IsFileAccessible function, it would probably be implemented as a giant try/catch block that attempted to open the file, caught failures, and returned the result.

阅读全文

相关推荐

最新文章