如何检查我正在处理的 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 ();
}
附:刚刚发现一个非常相似的问题,答案基本相同:
C#:有没有办法检查文件是否正在使用? https://stackoverflow.com/questions/876473/c-is-there-a-way-to-check-if-a-file-is-in-use
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)