开发者

How to run command line from Delphi?

How can I run this command from my Delphi application?

C:\myapppath\appfolder>appname.exe /stext save.txt

I tried the following code:

ShellExecute(0, nil, 'cmd.exe', 'cd C:\myapppath\appfolder', nil, SW_Hide);
ShellExecute(0, nil, 'cmd.exe', 'appname.exe /stext save.txt', nil, SW_Hide);

But it didn't wor开发者_C百科k. Can anyone provide a solution?


To run a CMD command, you need to use the /C flag of cmd.exe:

ShellExecute(0, nil, 'cmd.exe', '/C cd C:\myapppath\appfolder', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', '/C appname.exe /stext save.txt', nil, SW_HIDE);

However, this will create two different sessions, so it will not work. But you can use ShellExecute to run appname.exe directly, like so:

ShellExecute(0, nil, 'appname.exe',  '/stext save.txt', nil, SW_HIDE);

But you need to specify the filenames properly.

I would do

var
  path: string;

begin
  path := ExtractFilePath(Application.ExeName);
  ShellExecute(0, nil, PChar(Application.ExeName), PChar('/stext "' + path + 'save.txt"'), nil, SW_HIDE);
end;

in case appname.exe is the current application. Otherwise, replace Application.ExeName with the full path of appname.exe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜