如何检查文件是否打开 [英] How Can I Check Whether A File Is Open Or Not
本文介绍了如何检查文件是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static Boolean IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Truncate, FileAccess.Write, FileShare.None);
//using (StreamReader reader = new StreamReader(File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)))
//{
// return false;
//}
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
return false;
}
i尝试使用上面的代码,但它不起作用
i tried with above code but it's not working
推荐答案
如果你回去原始来源,据我所知,这里是: http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use [ ^ ]
它使用FileMode.Open,而不是FileMode.Truncate,而FileMode.ReadWrite则不是FileMode.Write - 它可能值得回到原始版本,因为它确实有效。
If you go back to the original source, which as far as I know is here: http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use[^]
It uses FileMode.Open, not FileMode.Truncate, and FileMode.ReadWrite not FileMode.Write - it's probably worth going back to the original, because that does work.
这篇关于如何检查文件是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文