有许多简单的方法可以在没有控制台窗口的情况下运行批处理文件。无需重新实现您的批处理文件。
一种不需要任何第 3 方软件的简单方法是使用 VBScript 或 JScript 脚本,该脚本使用参数设置为 0(= 隐藏窗口)的方法运行批处理文件WScript.Shell.Run
。intWindowStyle
引用@Shaji对如何在不启动“命令窗口”的情况下运行批处理文件的答案?:
将以下内容另存为 wscript,例如,将“testing.bat”替换为批处理文件的名称后的 hidecmd.vbs。
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
的第二个参数oShell.Run
是intWindowStyle
表示程序窗口外观的值,零值表示隐藏窗口。
参考在这里
https://docs.microsoft.com/en-us/previous-versions/d5fk67ky(v=vs.85)
其他方法见:
如果你真的需要一个 .exe,你可以使用 Inno Setup 来构建它。只需在InitializeSetup
事件函数中运行您的批处理文件,然后中止“安装”。结果.exe
将没有 GUI。
[Setup]
AppName=My Program
AppVersion=1.5
; Mandatory directive, but not actually used in this "installer"
DefaultDirName={pf}\My Program
#define SetupBatchFile "setup.bat"
[Files]
; Embed the batch file to the installer
Source: "{#SetupBatchFile}"; Flags: dontcopy
[Code]
function InitializeSetup(): Boolean;
var
BatchPath: string;
ResultCode: Integer;
begin
{ Extract the batch file }
ExtractTemporaryFile('{#SetupBatchFile}');
Log('Running batch file');
BatchPath := ExpandConstant('{tmp}\{#SetupBatchFile}');
if not Exec(BatchPath, '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then
begin
Log('Error running batch file');
end
else
begin
Log(Format('Batch file finished with exit code %d', [ResultCode]));
end;
Log('Exiting');
{ Prevents the actual Inno Setup installer from running }
Result := False;
end;
如果您的批处理文件不需要管理员权限,请将此指令添加到该[Setup]
部分:
PrivilegesRequired=lowest