«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

How to retrieve rich text from a resource file and save it to disk

   

These are the basic steps:

  1. Create a resource file
  2. Include it in your project
  3. Load the file from the resource file into a TResourceStream
  4. Create a TFileStream with the filename you want to write to
  5. Use CopyFrom to get the data from the TResourceStream to the TFileStream.
  6. Free both the streams

The file is magically written to disk, without any need to call a write procedure or anything like that. It takes a file called test.rtf from the resource file TEST.RES and saves it out to disk as test2.rtf in the application folder:

procedure TfrmMain.Button1Click(Sender: TObject);
var
  ResStream: TResourceStream
  MyFileStream: TFileStream;
begin
  try
    MyFileStream := TFileStream.Create(
      ExtractFilePath(Application.ExeName) + 'test2.rtf ',
      fmCreate or fmShareExclusive
    );
    ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);
    MyFileStream.CopyFrom(ResStream, 0);
  finally
    MyFileStream.Free;
    ResStream.Free;
  end;
end;
Original resource: The Delphi Pool
Author: Martin Holmes
Added: 2010-02-22
Last updated: 2010-02-22

« Return to contents »