gpt4 book ai didi

c# - 静默地从c#执行批处理文件

转载 作者:行者123 更新时间:2023-11-30 14:35:16 25 4
gpt4 key购买 nike

我知道之前有人问过这个问题,我已经尝试过这些帖子中给出的所有解决方案,但我似乎无法让它工作:-

static void CallBatch(string path)
{
int ExitCode;
Process myProcess;

ProcessStartInfo ProcessInfo;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/c " + path);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;

myProcess = Process.Start(ProcessInfo);
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.WaitForExit();

myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(process_Exited);

ExitCode = myProcess.ExitCode;

Console.WriteLine("ExitCode: " + ExitCode.ToString(), "ExecuteCommand");
myProcess.Close();
}

当我尝试调用批处理文件时,它仍然显示窗口,即使 createNoWindow 和 UseShellExecute 都设置为 true。

我是否应该添加其他内容以使其静默运行批处理文件?

最佳答案

试试这个:

Process myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c " + path;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(process_Exited);
myProcess.Start();
myProcess.WaitForExit();
ExitCode = myProcess.ExitCode;

想法是在启动进程后不要操作 myProcess.StartInfo:它是无用的。您也不需要将 UseShellExecute 设置为 true,因为您是通过调用 cmd.exe 自己启动 shell。

关于c# - 静默地从c#执行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12487452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com