Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Get file location

Tags:

path

delphi

To determine my exe path, I don't know which code I should use. Please give me explanation when and why should we use:

1. ExtractFilePath(ParamStr(0))

2. ExtractFilePath(Application.ExeName)

Because both code are rendering the same output.

C:\Users\Bianca\Documents\RAD Studio\Projects\Exam1\Win32\Release\
C:\Users\Bianca\Documents\RAD Studio\Projects\Exam1\Win32\Release\
like image 880
Bianca Avatar asked Jun 18 '14 09:06

Bianca


2 Answers

They both give you the same result, but there are subtle differences.

Application.ExeName references the VCL Application object. The use of this property requires you use the Vcl.Forms unit. Internally this does call the ParamStr(0) function.

Notice that the FireMonkey TApplication class does not have this property (as of XE5). So you cannot call Application.ExeName if you are using FireMonkey. And if you ever migrate a VCL project to FireMonkey you will have to rewrite this.

The ParamStr function OTOH is the System unit and is multiplatform (Win, Mac, iOS and Android, depending OC on the Delphi version you are using). On Windows ParamStr(0) calls the GetModuleFileName function, while on the other platforms it parses the command line returning the first token, which should be full path and name of running executable. (Thanks to Rob Kennedy for this correction)

So... I'd suggest you use ParamStr(0) directly.

like image 186
Frazz Avatar answered Sep 20 '22 14:09

Frazz


They are functionally identical. You would use ParamStr(0) if you didn't want the overhead of the Forms unit and all it's baggage.

like image 20
Andy_D Avatar answered Sep 20 '22 14:09

Andy_D