我想检查特定的 Excel 文件是否已打开。否则,当我在 C# 程序中重新打开同一文件时,它将以只读格式打开。有什么方法可以查出文件是否已经打开?
如果该文件被另一个程序打开,则此代码可以帮助您找出该文件,但您将无法打开它
<!-- language: c# -->
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(使用前将#替换为@)