Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I lock access to a file when a user has it open?

I'm writing a C#.NET program which uses XmlSerializer to serialize and deserialize the project the current user is working on to and from an XML file. This is working fine, but I'm trying to figure out a way to prevent two users from opening the same file off a network drive and having one user overwrite the previous user's save. I essentially want the behavior that MS Word has, where if the program can not gain write access to a file when it is being opened it opens the file in a read only mode.

So how do I lock access to a file? Do I just leave a file stream object open and keep a reference to it? Do I continue to use the same filestream object for any intermediate saves rather then opening and closing them for each save? Or is there some other way to lock access to a file?

Also how do I ensure that the file is released even if my app crashes? Would this be a good case to implement IDisposable?

like image 214
Eric Anastas Avatar asked Dec 02 '25 01:12

Eric Anastas


2 Answers

You can use the FileStream.Lock and FileStream.Unlock methods

like image 145
Mitch Wheat Avatar answered Dec 03 '25 16:12

Mitch Wheat


You don't have to do anything special to make this work. First try to open the file with FileAccess.ReadWrite and FileShare.Read. If you don't get an IOException, you'll have exclusive write access to the file. Other processes can read from the file but can never gain write access.

If that open attempt fails, you can assume that somebody else already has the file open for writing. Now switch your internal program logic to "read-only" mode and open the file with FileAccess.Read and FileShare.ReadWrite. If that still bombs then there's something fundamentally wrong with the file, the IOException tells you what.

Be very careful to avoid an "is the file locked" test. That can't work reliably on a network, you have to keep the file opened after gaining access to it. As soon as you close it, another process can gain access to it, a nanosecond later.

Another typical problem with this kind of locking is that the user typically wants to know who's got a lock on the file. Windows doesn't provide any standard way to find out. Office deals with this by creating a hidden file in the same directory with the same name as the file but another filename extension. It writes the name of the user in this file, available to any process that finds the file locked. You can do the same, Environment.UserName would be good for that.

like image 21
Hans Passant Avatar answered Dec 03 '25 14:12

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!