如何检查文件是否在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#:有没有办法检查文件是否正在使用?
- 最小化屏幕时C#LinearGradientBrush毛刺
- 在我的.NET代码通过dllimport调用非托管dll中的函数之后,有没有办法调试正在发生的事情?
- 特定于文化的DateTime字符串在平台之间不一致
- (0 ==变量)或(null == obj):C#中过时的练习?
- 如何在使用自定义DataSource时隐藏DataGridView的列?
- 如何将wchar_t *从C ++编组为C#作为out参数或返回值?
- 以编程方式调用EntityDeploy构建任务
- 清单资源的资源名称是什么(Assembly.GetManifestResourceStream)?
- var in C# – 为什么它不能用作成员变量?