如何检查文件是否在c#中使用?

如何检查我正在使用的excel文件(操作其数据,删除或覆盖它)是否正被另一个程序使用? 以及如何从同一版本中释放它?

请指导我使用C#。

尝试用“写作”标志打开。 如果失败,则该文件被其他进程“占用”。

FileStream fileStream = null; try { fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Write); } catch (UnauthorizedAccessException e) { // The access requested is not permitted by the operating system // for the specified path, such as when access is Write or ReadWrite // and the file or directory is set for read-only access. } finally { if (fileStream != null) fileStream.Close (); } 

PS刚刚找到一个非常相似的问题,回答基本相同:

C#:有没有办法检查文件是否正在使用?