whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

47 Visitors Online


 
...obtain the path to your program at runtime?
Autor: Marc Dürst
Homepage: http://www.idev.ch
[ Print tip ]  

Tip Rating (7):  
     


{*****************************************
Deutsch:
******************************************}

Die meisten von Euch kennen vermutlich die die Methode

MyFileName := Application.ExeName

mit welcher Ihr den Namen der aktuellen EXE oder DLL abfragen könnt. Soweit so gut. Der Haken dabei ist, dass dies bei Diensten, Control-Panel-Apps noch bei Web-Apps funktioniert, da diese "Application's" besagte Methode nicht haben. Eine Methode (aus den frühen Pascal-Zeiten), welche immer funktioniert ist das Kommando:

MyFileName := ParamStr(0);

Richtig! Es handelt sich um den 0-ten Kommandozeilenparameter. Windows macht das so, das dies der eigene Name ist. Wenn Ihr also in jedem Fall den Pfad zu Eurem Programm zur Laufzeit ermitteln wollte, geht das beispielsweise so:

MyFilePath := ExtractFilePath(ParamStr(0));


{*****************************************
ENGLISH:
******************************************}

As most of you know the method

MyFileName := Applicatoin.ExecName;

to get the filename (incl. path) to the current executable (exe or dll). Well, this is ok but doesn't work for things like service-apps, controlpanel-apps, web-apps, etc. as the application object of thouse doesn't have the ExecName property. A function which works in every case is

MyFileName := ParamStr(0);

You're right! It's the command line parameter number zero. This is the way Windows does it. If you'd like to get the path to your program at runtime your can do something like:

MyFilePath := ExtractFilePath(ParamStr(0));

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners