Open file in exclusive mode

F

Frank Rizzo

I am trying to open a file in exclusive mode for writing. Is the code
below correct?

FileStream oStream = File.Open(FileName, FileMode.Open,
FileAccess.Write, FileShare.None);

The purpose is two-fold:
1. I want no one to be able to open a file while I am operating on it.
2. If the file is open by someone else, I want the code above to throw
an exception.

Will it do the trick?
Thanks.
 
D

David Browne

Frank Rizzo said:
I am trying to open a file in exclusive mode for writing. Is the code
below correct?

FileStream oStream = File.Open(FileName, FileMode.Open, FileAccess.Write,
FileShare.None);

The purpose is two-fold:
1. I want no one to be able to open a file while I am operating on it.
2. If the file is open by someone else, I want the code above to throw an
exception.

Will it do the trick?

Yep.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top