If you use the FileShare parameter then the file will be locked to anyone
else until the file is closed (by disposing of the FileStream). The
Lock/Unlock methods allow you lock/unlock certain regions of a file where
that file allows write access to others.
--
Keith
"Joris van Lier" <whizzrd@hotmail.com> wrote in message
news:etPbqbwlHHA.4188@TK2MSFTNGP02.phx.gbl...
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