Discussion:
lock file access
(too old to reply)
unknown
2005-11-29 04:23:10 UTC
Permalink
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.
Truong Hong Thi
2005-11-29 05:01:54 UTC
Permalink
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
unknown
2005-11-30 03:27:21 UTC
Permalink
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" <***@gmail.com> wrote in message news:***@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
Truong Hong Thi
2005-11-30 04:14:52 UTC
Permalink
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

Loading...