You can suffer from a thread race condition on this which there are documented examples of this being used as a security vulnerability.
(您可能因此而遭受线程争用的情况,有记录在此的示例被用作安全漏洞。)
If you check that the file is available, but then try and use it you could throw at that point, which a malicious user could use to force and exploit in your code. (如果您检查该文件是否可用,然后尝试使用它,则可能会丢掉该文件,恶意用户可能会使用该文件来强制和利用您的代码。)
Your best bet is a try catch / finally which tries to get the file handle.
(最好的选择是try catch /最终尝试获取文件句柄。)
try
{
using (Stream stream = new FileStream("MyFilename.txt", FileMode.Open))
{
// File/Stream manipulating code here
}
} catch {
//check here why it failed and ask user to retry if the file is in use.
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…