[Mono-list] Error when attempting to lock file

Rich Rattanni rattanni at gmail.com
Mon May 24 09:55:53 EDT 2010

I run the following code:
------- BEGIN CODE ------
using System;
using System.IO;
namespace fileipc
  class MainClass
    public static void Main (string[] args)
      FileStream fs = new FileStream("/var/tmp/ipc.txt",
FileMode.Open, FileAccess.Write, FileShare.Read);
      fs.Lock(0, Int64.MaxValue);
      fs.Unlock(0, Int64.MaxValue);
------- END CODE ------
On mono and 2.6.3 running on Linux kernel and get
the following error (output is from 2.6.3).

------ BEGIN ERROR -----
Unhandled Exception: System.IO.IOException: Lock violation on path
  at System.IO.FileStream.Lock (Int64 position, Int64 length)
[0x00000] in <filename unknown>:0
  at fileipc.MainClass.Main (System.String[] args) [0x00000] in
<filename unknown>:0
------ END ERROR -----
I have made sure that /var/tmp/ipc.txt exists first before executing
this program.

I have seen information from web searches suggesting there are issues
with FileStream.Lock(), but the information I am reading is sketchy at
best.  Could someone confirm if problems exist with file locking and
mono, and suggest any possible alternatives?

(I am wondering how the SQLite extension accomplishes file
locking...does my solution lay here?)

More information about the Mono-list mailing list