Search code examples
delphiparamstr

Why does ParamStr show the wrong file name?


I have associated the ".file_5" extension with my application and I used the ParamStr(1) function in Delphi to show a messagebox that contains the path & file name of the file when I double click on it in explorer using the code below.

procedure TForm1.FormCreate(Sender: TObject);
 var
  TheFile : string;
begin
  TheFile := ParamStr(1);  //filename for the file that was loaded
  ShowMessage(TheFile);
end;

This works, but if I move the file to another location then the location it was originally in, then the message that is shown is not correct.

Example: (using test.file_5)

The original location of the file is in the C:\ drive and when I double click it my application starts and displays a Messagebox that says:

C:\test.file_5

This is correct. If I move that same file to a directory that contains spaces like the program file folder for example then the Messagbox that is displayed is not

C:\Program Files\test.file_5

like I would expect but rather

C:\PROGRA~1.FILE_

which is obviously not the information I am after so my question is how can I use the ParamStr() function to take into account directories that have spaces in them or is there a better function that I should use that works with directories that contains spaces in them.


Solution

  • Your association is set up wrong. Instead of having double_clicking the .file_5 doing

    C:\YourPath\YourApp.exe %1
    

    The association should be set up as

    "C:\YourPathYourApp.exe" "%1"
    

    Notice the double quotes around the %1 - this keeps any spaces contained instead of their causing Windows to pass the short path and filename.