The Road to Delphi

Delphi – Free Pascal – Oxygene

Using ParamStr in Delphi Prism

Leave a comment

.Net does not include the ParamStr Function (The ParamStr function returns one of the parameters from the command line used to invoke the current program), but can be easily implemented, here is an example :

class method TMyClass.ParamStr(Index: Integer): String;
var
  MyAssembly: System.Reflection.Assembly;
  Params    : array of string;
begin
  if Index = 0 then
  begin
    MyAssembly:= System.Reflection.Assembly.GetEntryAssembly;
    if Assigned(MyAssembly) then
      Result := MyAssembly.Location
    else
      Result := System.Diagnostics.Process.GetCurrentProcess.MainModule.FileName;
  end
  else
  begin
    Params := System.Environment.GetCommandLineArgs;
    if Index > Length(Params) - 1 then
      Result := ''
    else
      Result := Params[Index];
  end;
end;

Unknown's avatar

Author: Rodrigo

Just another Delphi guy.

Leave a comment