有没有办法以编程方式检查是否打开Excel文件

我想检查一个特定的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; } 

(你不能用它做任何事情,文件必须从程序中closures,打开它)