加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何从Delphi运行命令行?

发布时间:2020-12-15 09:27:06 所属栏目:大数据 来源:网络整理
导读:如何从我的Delphi应用程序运行此命令? C:myapppathappfolderappname.exe /stext save.txt 我尝试了以下代码: ShellExecute(0,nil,'cmd.exe','cd C:myapppathappfolder',SW_Hide);ShellExecute(0,'appname.exe /stext save.txt',SW_Hide); 但它没有用.
如何从我的Delphi应用程序运行此命令?

C:myapppathappfolder>appname.exe /stext save.txt

我尝试了以下代码:

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

但它没有用.有人能提供解决方案吗?

解决方法

要运行CMD命令,需要使用cmd.exe的/ C标志:

ShellExecute(0,'/C cd C:myapppathappfolder',SW_HIDE);
ShellExecute(0,'/C appname.exe /stext save.txt',SW_HIDE);

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

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

但是您需要正确指定文件名.

我会做

var
  path: string;

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

以防appname.exe是当前的应用程序.否则,将Application.ExeName替换为appname.exe的完整路径.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读