另一个进程使用的文件 [英] File used by another process
问题描述
很多次在Windows平台上尝试写入文件时出现错误
Many a times we get an error, while trying to write file on Windows platform,
该进程无法访问文件'XXX',因为它正在被另一个进程使用."
"The process cannot access the file 'XXX' because it is being used by another process."
在将文件写入其他进程未使用的文件之前,如何检入C#?
How to check in C#, before writing to file that its not being used by another process?
推荐答案
基本上,您不能-必须尝试打开文件进行写入,如果遇到异常,则无法写入该文件:(
You can't, basically - you have to attempt to open the file for writing, and if you get an exception you can't write to it :(
即使您可以先进行单独的检查,结果在开始编写之前也会过时-以后仍然可能出现异常.
Even if you could do a separate check first, the result would be out of date before you could start writing - you could still end up with the exception later.
诚然,如果框架提供了 TryOpen
方法,那就太好了……
It would be nice if the framework provided a TryOpen
method, admittedly...
这篇关于另一个进程使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!