G
Guest
Hi!
I'm having problems reading a partially locked file. The writer app (a 3rd
party app) writes new data at the end of the file every second and sets a
file lock for the last record which could be 160 bytes.
My app (the reader app) tries to read the new data but since FileStream is
buffered I get IOException "the portion is locked by another process" when
trying to read an unlocked area of the file. It seems that the buffer size is
4kb. The result is a lock exception when I try to read 1 byte that is within
4 kb before the real lock and that is not nice!
Of course the FileStream should not throw a lock exception when reading an
unlocked byte with a lock 3-4 kb ahead.
Using an unbuffered FileStream should propably solve the problem, but I
cannot find such stream in .NET. The nearest I can get is to specify the
buffer size when creating the FileStream, but when specifying 0 it well be
set to 8 automatically which means that I cannot read the last 8 bytes before
the lock. And of course using unbuffered stream slows down a bit.
Obviously there is a bug in the buffered stream, it should not throw when
reading ahead.
Any ideas someone ?
Some code:
Dim fInfo as New FileInfo(filename)
Dim fs as FileStrem
fs = fInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim br as New BinaryReader(fs)
br.BaseStream.Position(<an unlocked address>)
br.ReadByte() ==> IOException: "The process cannot access the file because
another process has locked a portion of the file."
I'm having problems reading a partially locked file. The writer app (a 3rd
party app) writes new data at the end of the file every second and sets a
file lock for the last record which could be 160 bytes.
My app (the reader app) tries to read the new data but since FileStream is
buffered I get IOException "the portion is locked by another process" when
trying to read an unlocked area of the file. It seems that the buffer size is
4kb. The result is a lock exception when I try to read 1 byte that is within
4 kb before the real lock and that is not nice!
Of course the FileStream should not throw a lock exception when reading an
unlocked byte with a lock 3-4 kb ahead.
Using an unbuffered FileStream should propably solve the problem, but I
cannot find such stream in .NET. The nearest I can get is to specify the
buffer size when creating the FileStream, but when specifying 0 it well be
set to 8 automatically which means that I cannot read the last 8 bytes before
the lock. And of course using unbuffered stream slows down a bit.
Obviously there is a bug in the buffered stream, it should not throw when
reading ahead.
Any ideas someone ?
Some code:
Dim fInfo as New FileInfo(filename)
Dim fs as FileStrem
fs = fInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim br as New BinaryReader(fs)
br.BaseStream.Position(<an unlocked address>)
br.ReadByte() ==> IOException: "The process cannot access the file because
another process has locked a portion of the file."