开发者

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 referenc开发者_Go百科e 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?


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


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.


If you are using the File.Open method to open the file stream to your files, make sure you use the overload that takes a FileShare parameter (this one). You can set this to various values to specify how the stream should lock the file whilst in use (ie. allow reading by other processes but not writing, prevent any use by other processes). You need the None value to prevent any other process (or another stream in your process) from accessing the file.

To ensure the file is released, make sure you close the FileStream when you have finished with it. To ensure it is closed even if your application crashes, you don't need to do anything specific - if your application crashes, its process will terminate and the lock will be released.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜