Search code examples
delphidelphi-7

Run open file with Opendialog and execute it


I want to do something like this:

ShellExecute(0, nil, PChar('E:\generic\mpv\mpv.exe'), PChar(''+TntOpenDialog1.FileName+''), nil, SW_HIDE);

but nothing happened.
When I try to do something with code like this:

TntLabel1.Caption:=PChar(TntOpenDialog1.FileName);

it just shows me the first letter of file which I selected before.
How can I solve this problem?


Solution

  • if you space in the file path expect, you can not pass a param like ''+myparam+''

    var
    myparam : AnsiString;
    begin
    myparam := 'test file nr 10.txt';
    ShellExecute(0, nil, PChar('...'), PChar(''+myparam+''), nil, SW_HIDE);
    

    ParamStr(1) will give you only test !!

    If you have spaces in the path use it like:

    ShellExecute(0, nil, PChar('...'), PChar('"'+myparam+'"'), nil, SW_HIDE);
    

    Try following. but take care with the cast from FFileName: WideString to AnsiString, information may be lost. Use this only if there are no special characters in the file path occur.

    ...
    var
    AnsiStr : AnsiString;
    ...
    begin
    
    AnsiStr := TntOpenDialog1.FileName;
    ShellExecute(0, nil, PChar('E:\generic\mpv\mpv.exe'), PChar('"'+AnsiStr+'"'), nil, SW_HIDE);