简体   繁体   中英

Prevent Excel from causing my app to crash with file lock error

I open a CSV file from within my C# application for writing with a

FileShare.Read 

flag. I only ever append to the file - I never attempt random access. If I open the file from Excel while my app is still running, Excel gives me an expected "open read-only/notify" prompt. If I chose 'Notify', occasionally my app fails while attempting to write a new line to the file with a

0x21 error - "The process cannot access the file because another process has locked a portion of the file."

I assumed allowing other processes to read the file was safe, but apparently not. Is there anyway to share the file for reading while at the same time prevent another app from locking it (I'm assuming Excel tries to copy the file causing the lock)? I'm also assuming a lock would only affect the bytes at the time of the copy - so why would appending to the file affect that?

当您访问该文件时,会有一个标志来执行悲观锁定而不是乐观锁定。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM