View Single Post
Old 05-15-2007   #9 (permalink)
Joris van Lier


 
 

Re: Open file for exclusive access

Keith Hill wrote:
> "char1iecha1k" <charlesgargent@gmail.com> wrote in message
> news:1179176871.496647.42740@l77g2000hsb.googlegroups.com...
>> Hi,
>>
>> I would like to open a file for exclusive access for the duration of
>> a script. If the script fails or finishes the lock must be released.
>> This is to prevent another user or process from accessing the same
>> data file. I have googled to no avail.
>>
>> Thanks in advance
>>

>
> Perhaps you should try this FileStream constructor:
>
> public FileStream (
> string path,
> FileMode mode,
> FileAccess access,
> FileShare share
> )
>
> And set the share parameter to FileShare.None.


Good suggestion,

Is the FileShare.None option enough to lock the file or should one call $fileStream.Lock(0,$filestream.Length-1)?

And how about unlocking the file, will dereferencing the FileStream lift the lock, or is an explicit call to unlock required/recommended?


--
Joris van Lier
Please note that all scripts and opinions are supplied "as is" and with
no warranty Blog: http://whizzrd.spaces.live.com

My System SpecsSystem Spec