可能的重复:
有没有办法检查文件是否正在使用? https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
检查文件是否打开 https://stackoverflow.com/questions/2987559/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;
}
我发现上面的代码不能正常工作
摘自这里:有没有办法检查文件是否正在使用? https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)