I'm using system.io.file How do I lock access to the file while appending?
Other processes may try to open it while I want it.

Thanks,
Colin.

Re: lock file access by Truong

Truong
Mon Nov 28 23:01:54 CST 2005

Hi Colin,

By default, if you open a file for writting, it is locked and not
others cannot write to the same file until you close it. It you also
don't want others to read, you could pass FileShare.None to the
constructor of FileStream.

FileStream stream = new FileStream(name, FileMode.Append,
FileAccess.Write, FileShare.None);
StreamWriter w = new StreamWriter(stream);

Regards,
Thi - http://thith.blogspot.com


Re: lock file access by Colin

Colin
Tue Nov 29 21:27:21 CST 2005

I understand my problem better now. When another process has a lock on the
file, I would like to wait until the lock is released and then do my append.
How can I do this?

Thanks,
Colin.


"Truong Hong Thi" <thi1981@gmail.com> wrote in message
news:1133240514.903637.185110@z14g2000cwz.googlegroups.com...
Hi Colin,

By default, if you open a file for writting, it is locked and not
others cannot write to the same file until you close it. It you also
don't want others to read, you could pass FileShare.None to the
constructor of FileStream.

FileStream stream = new FileStream(name, FileMode.Append,
FileAccess.Write, FileShare.None);
StreamWriter w = new StreamWriter(stream);

Regards,
Thi - http://thith.blogspot.com



Re: lock file access by Truong

Truong
Tue Nov 29 22:14:52 CST 2005

Hi Colin, the way I often do is waiting for some time (Thread.Sleep)
and try again, may need to try several time.

Hope it helps,
Thi