如何从Delphi运行命令行? [英] How to run command line from Delphi?

查看:112
本文介绍了如何从Delphi运行命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Delphi应用程序中运行此命令?

How can I run this command from my Delphi application?


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

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

我尝试了以下代码:

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 work. Can anyone provide a solution?

推荐答案

要运行CMD命令,您需要使用 / C 标记 cmd.exe

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);

但是,这将创建两个不同的会话,因此它将无法正常工作。但是可以使用ShellExecute直接运行 appname.exe ,如下所示:

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.

我会做

var
  path: string;

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

如果 appname.exe 是当前应用。否则,使用完整路径 appname.exe 替换 Application.ExeName

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

这篇关于如何从Delphi运行命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆