4

i want to embedded a file (any kind of type) to my exe application and be able to extract in the remote to use it, i know how do it by embedded into resource,but i don't want to place the files in the app directory, i want to store all files (like .rec) into my exe, in c# it is possible to store as text file and then read it by FileStream but in Delphi the resource files is separate from the exe file. is there any solution to do this ? Thanks a lot!

2
  • 4
    You've misunderstood something. In Delphi, as in all development environments, the resources are separate during development. After you compile and link your program, the resources are in the EXE file. You can do the same in C#, although then you wouldn't use a FileStream to read it because you wouldn't want to read your entire EXE file just to get your embedded file. Commented Feb 19, 2010 at 17:30
  • Also be aware that you might need administrative privileges on the target machine if you want to run this executable after extracting it on Vista/Win7. Commented Feb 19, 2010 at 17:50

3 Answers 3

17

You should make an .rc file and add that to your project. The content of the RC file is like:

FIXED48                 IMAGE           ..\Resources\Fixed48x48.png
MENU16                  IMAGE           ..\Resources\Menu16x16.png
TICK            SOUND       ..\Resources\Tick.wav
PING            SOUND       ..\Resources\Ping.wav

Now after you do a build you can load one of these fikles using a TResourceStream:

procedure TdmReportGeneral.InsertLogo(Memo: TStringList; View: TfrView);
var
    S:    TResourceStream;
begin
    if (View is TfrPictureView) and (View.Name = 'Logo') then begin
        S := TResourceStream.Create( 0, 'FIXED48', 'IMAGE' );
        try
            // do something useful... TfrPictureView(View).Picture.MetaFile.LoadFromStream( S );
        finally
            S.Free();
        end;
    end;
end;
Sign up to request clarification or add additional context in comments.

Comments

2

You should be able to get the Delphi compiler to link your resource into your EXE by adding it as a {$R myresource.res} pragma in a unit in your project. You can then get a handle to it via a call to FindResource when you need to read it.

This article takes you through the appropriate steps.

1 Comment

my question is this that how to add the rec file into the project (i don't want to make a link to it from a directory) i want that my published exe just be one .exe file,without any dependence
2

DelphiDabbler has a great article on this very topic. They even include 2 example projects for download that show how to embed a file as a resource, and how to read it back.

You can download a worked example that demonstrates what we've described here – it uses the above code. The zip file contains a pair of projects. The first is a program that embeds a supplied rich text file in a resource file. The second program includes the resource file and displays the rich text in a rich edit component as above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.