Link to home
Start Free TrialLog in
Avatar of petervn
petervn

asked on

how to et current file name and path

how to know the path and file name of my project while it's running?
i'm afraid that someone will rename it
Avatar of cwp
cwp

The path and filename of your program will be stored in the ParamStr(0) variable.
ASKER CERTIFIED SOLUTION
Avatar of gmayo
gmayo
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Whoops. Did I say variable? I meant function.
var FilePath,FileName:string;
...

FilePath:= ExtractFilePath(Application.ExeName);
FileName:= ExtractFileName(Application.ExeName);
Application.ExeName internally calls ParamStr(0), but it doesn't work with console apps (or wherever you don't have the TApplication object), so I'd rather follow Geoff's hint.
Windows automatically adds the path + filename of a launched app as the first parameter (ParamStr() is zero-based).

Markus
var FilePath,FileName:string;
...

FilePath:= ExtractFilePath(Application.ExeName);
FileName:= ExtractFileName(Application.ExeName);

or

//only for app not usage tapplication

FilePath:= ExtractFilePath(ParamStr(0));
FileName:= ExtractFileName(ParamStr(0));

:-D
Now that is b*stardisation if ever I saw it...

Geoff M.

Taken from Key Objects Library (KOL)

function GetStartDir : String;
var Buffer:array[0..260] of Char;
    I : Integer;
begin
  I := GetModuleFileName( 0, Buffer, Sizeof( Buffer ) );
  for I := I downto 0 do
    if Buffer[ I ] = '\' then
    begin
      Buffer[ I + 1 ] := #0;
      break;
    end;
  Result := Buffer;
end;