【发布时间】:2012-02-14 13:12:37
【问题描述】:
可能重复:
Is there a way to check if a file is in use?
Check if a file is open
我怎么知道文件已经打开或正在使用。
public bool FileIsLocked(string strFullFileName)
{
bool blnReturn = false;
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(strFullFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
fs.Close();
}
catch (System.IO.IOException ex)
{
blnReturn = true;
}
return blnReturn;
}
我发现上面的代码不能正常工作
【问题讨论】:
-
请注意,使用这样的函数获得的信息可能会在您使用它的那一刻过时。该文件可以被锁定,您检查的实例不是,反之亦然。通常最好尝试对文件执行任何操作,并在错误发生时做出反应。
-
我得到了这个代码,但它不能正常工作