Craig
Craig

Reputation: 573

How can I make the program save itself in Delphi 7

How can I make my Delphi 7 program save itself into a new location. For example the program is saved on my flash stick then when I run it I want it to save itself in c:\user \ (user name)

The above is the original question.

This is the code I tried using. The program runs perfectly but when I check the folder it was supposed to save it in it does not appear there.

 procedure TForm5.FormActivate(Sender: TObject);
  var source, dest : string;
 begin
  Source := 'project1.exe';
  Dest := 'C:\Users\Craig\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup';
  CopyFile(PChar(Source), PChar(Dest), False);
 end;

Thanks for your help.

Upvotes: 2

Views: 1312

Answers (2)

David Heffernan
David Heffernan

Reputation: 612914

Read the executable file name from Application.ExeName. And then call CopyFile to perform the copy.

Source := Application.ExeName;
Dest := ...;
CopyFile(PChar(Source), PChar(Dest), False);

Regarding your update:

  1. The destination needs to be a file. You are attempting to copy a file to a path that specifies a folder.
  2. You didn't check for errors. When you call an API function like CopyFile, you need to check the return value.

You want something like this:

Source := 'project1.exe';
Dest := 'C:\Users\...\Startup\project1.exe';
if not CopyFile(PChar(Source), PChar(Dest), False) then
  RaiseLastOSError;

Upvotes: 3

Omair Iqbal
Omair Iqbal

Reputation: 1838

try this:

CopyFile(PChar(C:\OldFile.exe), PChar(C:\NewFile.exe), true);

Upvotes: 0

Related Questions