【问题标题】:Prevent Excel from causing my app to crash with file lock error防止 Excel 导致我的应用程序因文件锁定错误而崩溃
【发布时间】:2017-02-08 23:16:52
【问题描述】:

我从我的 C# 应用程序中打开一个 CSV 文件以使用

FileShare.Read 

标志。我只附加到文件中 - 我从不尝试随机访问。如果我在我的应用程序仍在运行时从 Excel 打开文件,Excel 会给我一个预期的“打开只读/通知”提示。如果我选择“通知”,有时我的应用程序会在尝试使用

向文件写入新行时失败
0x21 error - "The process cannot access the file because another process has locked a portion of the file."

我认为允许其他进程读取文件是安全的,但显然不是。无论如何要共享文件以供读取,同时防止另一个应用程序锁定它(我假设 Excel 尝试复制导致锁定的文件)?我还假设锁定只会影响复制时的字节 - 那么为什么附加到文件会影响呢?

【问题讨论】:

    标签: c# excel file locking share


    【解决方案1】:

    当您访问文件时,有一个标志来执行悲观锁与乐观锁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-07
      • 2022-11-10
      • 2018-12-11
      • 2018-05-22
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多