How can you open a file with exclusive access so no other process can read/write it?
home / developersection / forums / how can you open a file with exclusive access so no other process can read/write it?
How can you open a file with exclusive access so no other process can read/write it?
ICSM Computer
30-May-2025To open a file with exclusive access in C#, you can use the
FileStreamconstructor and explicitly set theFileSharemode toNone.Example: Open a file with exclusive access
FileShare.Nonemeans:No other process or thread can open the file — not for reading, writing, or even deleting — until the stream is closed.
Usage
Summary of
FileShareOptionsNoneReadWriteReadWriteDeleteNotes
usingblock orDispose()to release the lock promptly.FileShare.None, it will throw anIOException.fcntl).