Title: Read from a busy file
Question: There are some applications not very friendly with file sharing. How can I read data from such a file without causing any troubles to the other application?
Answer:
Even when openning your file with the lowest privileges (using ReadOnly, ShareReadWrite) sometimes opening an already open file can be a hard work to do, specially if the file is highly active (constant reading and writing from the other app). A very simple and elegant solution is to use a MemoryStream instead of accesing the file directly:
var Memory : TMemoryStream;
begin
Memory := TMemoryStream.Create;
try
Memory.LoadFromFile('busyfile.dat'); // that's it!!
..
Memory.Read(...); // and you can use read methods just like in files
Memory.Seek(...);
FileSize := Memory.Size;
..
finally
Memory.Free;
end;
end;
This way you never open the file, but make a copy of it in Memory. Of course you can also write to the Stream in Memory, but the changes won't be reflected in disc until you save it to a file (with SaveToFile).
Juan Antonio Navarro - tres1416