在 Delphi 中,可以使用 WinExec()
或 ShellExecute()
函数来执行 cmd 命令。
WinExec
代码如下
var
CommandLine: string;
begin
CommandLine := 'cmd.exe /c dir';
WinExec(PChar(CommandLine), SW_SHOWNORMAL);
end;
代码使用了 WinExec()
函数,并传入要执行的命令字符串 'cmd.exe /c dir'
。SW_SHOWNORMAL
参数指定了窗口的显示方式。
ShellExecute
代码如下
uses ShellAPI;
var
CommandLine: string;
begin
CommandLine := 'cmd.exe /c dir';
ShellExecute(0, nil, 'cmd.exe', PChar('/c ' + CommandLine), nil, SW_SHOWNORMAL);
end;
代码使用了 ShellExecute()
函数,并传入要执行的命令字符串 'cmd.exe /c dir'
和参数 /c
。ShellExecute()
函数还支持多种执行方式,例如以最小化或隐藏窗口等方式执行。