file IO, only 1 update at a time

  • Thread starter Thread starter andrewcw
  • Start date Start date
A

andrewcw

I am sure there is an easy solution to this. I have an
absolute bullet proof requirement that only 1 application
at a time can update a file.

I know that if I Use:
System.IO.Stream fs= File.OpenWrite(fiPath);
No on else can open the file even for reading, (
unfortunately I can't either )

but what I need is to open the file exclusively, read the
text exclusively, then update it - and keep other
appliucations out until the update is done.

If I read first then the argument is being made that some
other application could have gotten in an changed the
content.

Can anyone share there snippet with me ? Thanks
 
andrewcw wrote:
|| I am sure there is an easy solution to this. I have an
|| absolute bullet proof requirement that only 1 application
|| at a time can update a file.
||
|| I know that if I Use:
|| System.IO.Stream fs= File.OpenWrite(fiPath);
|| No on else can open the file even for reading, (
|| unfortunately I can't either )
||
|| but what I need is to open the file exclusively, read the
|| text exclusively, then update it - and keep other
|| appliucations out until the update is done.
||
|| If I read first then the argument is being made that some
|| other application could have gotten in an changed the
|| content.
||
|| Can anyone share there snippet with me ? Thanks

Use:
System.IO.Stream fs FileStream(fiPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

That way you open the file for update and prevent others to open the file (exclusive open).

Willy.
 
Back
Top