[Mono-list] File Locking

Andy York andy at brdstudio.net
Wed Feb 17 15:17:06 UTC 2016


Have you tried checking out other project in GitHub or CodePlex? I've 
gotten some really good tips from reading other cross-platform code that 
deals with locking files. Just a suggestion.
  
  
  
  
  

----------------------------------------
 From: "Edward Ned Harvey (mono)" <edward.harvey.mono at clevertrove.com>
Sent: Wednesday, February 17, 2016 9:46 AM
To: "shrimpywu" <shrimpywu at hotmail.com>, "mono-list at lists.ximian.com" 
<mono-list at lists.ximian.com>
Subject: Re: [Mono-list] File Locking   
> From: mono-list-bounces at lists.ximian.com [mailto:mono-list-
> bounces at lists.ximian.com] On Behalf Of shrimpywu
>
> i have the same issue. any solution?

Kinda sorta. The underlying issue is that OSX, Linux, and Windows all have 
different underlying file locking constructs, and then of course, there's 
some variability about even *which* filesystem is being used. I didn't 
thoroughly figure out all the answers for every OS or filesystem, and I 
don't know under which situations this will be good enough, but this is 
what I ended up using, works under the conditions we needed it to work:

using (var foo = new FileStream(filePath, FileMode.Open, 
FileAccess.ReadWrite, FileShare.None)) { // must include Write access in 
order to lock file
foo.Lock(0, 0); // 0,0 has special meaning to lock entire file regardless 
of length
}

For windows, simply specifying the FileAccess and FileShare is good enough. 
For linux, at least ext4, files are concurrently readable regardless of 
what you specify for FileAccess and FileShare. The Lock() method does 
something of a soft-lock. It's not enforced by the OS, but at least all the 
situations we tried, other client apps honor the lock. Didn't look into it 
any deeper.
_______________________________________________
Mono-list maillist - Mono-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20160217/c20f4eb6/attachment.html>


More information about the Mono-list mailing list