2010. március 10., szerda
How to save a TCollectionItem from a component to a stream
Problem/Question/Abstract:
I have written a component which uses TCollections as properties. Now I need a way to save the TCollectionItems to a file. How can I do that?
Answer:
You may try these routines. I have tested them with TStatusBar.Panels collection and they worked for me:
procedure LoadCollectionFromStream(Stream: TStream; Collection: TCollection);
begin
with TReader.Create(Stream, 4096) do
try
CheckValue(vaCollection);
ReadCollection(Collection);
finally
Free;
end;
end;
procedure LoadCollectionFromFile(const FileName: string; Collection: TCollection);
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadCollectionFromStream(FS, Collection);
finally
FS.Free;
end;
end;
procedure SaveCollectionToStream(Collection: TCollection; Stream: TStream);
begin
with TWriter.Create(Stream, 4096) do
try
WriteCollection(Collection);
finally
Free;
end;
end;
procedure SaveCollectionToFile(Collection: TCollection; const FileName: string);
var
FS: TFileStream;
begin
FS := TFileStream.Create(FileName, fmCreate or fmShareDenyWrite);
try
SaveCollectionToStream(Collection, FS);
finally
FS.Free;
end;
end;
Note: It's obvious, the Collection variable must point to the initialized instance of a TCollection
descendant.
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése