open file in exclusive mode in C#

后端 未结 5 1085
时光说笑
时光说笑 2020-11-30 08:40

I want to open a file for read in exclusive mode, and if the file is already opened by some process/thread else, I want to receive an exception. I tried the following code,

5条回答
  •  情话喂你
    2020-11-30 09:39

    I would suggest using the FileAccess.ReadWrite member because some files may already be open but allow you Read access on the file. However, I would guess that in non-exceptional conditions, all files open for Read/Write access would not allow your code to Write to the file.

    Of course (as Mehrdad already explained), if you are using an editor such as Notepad to open the file as a test, you will not be able to restrict access because Notepad does not lock the file at all.

提交回复
热议问题