C#,如何在没有弹出控制台窗口的情况下运行.bat文件

Had*_*lak 4 c# batch-file

我正在尝试运行.bat没有弹出控制台窗口的文件.

我正在使用此代码:

Process p = new Process();

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "file.bat";
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

使用此代码,程序会弹出控制台窗口一秒钟并消失.如何获得它永远不会显示?

ghu*_*ali 13

加上

p.StartInfo.CreateNoWindow=true;
Run Code Online (Sandbox Code Playgroud)

控制台窗口弹出窗口不会出现


Mag*_*ken 6

p.StartInfo.CreateNoWindow = true;
Run Code Online (Sandbox Code Playgroud)


Joh*_*nFx 5

另一种选择是

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Run Code Online (Sandbox Code Playgroud)

要使用ProcessWindowStyle.Hidden,该ProcessStartInfo.UseShellExecute属性必须为false,但看起来您正在使用该设置,因此您应该很好.