您可以告诉进程不使用窗口或将其最小化:
// don't execute on shell
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
// don't show window
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
您可以UseShellExecute = false
重定向输出:
// redirect standard output as well as errors
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
当你这样做时,你应该使用输出缓冲区的异步读取来避免由于缓冲区溢出导致的死锁:
StringBuilder outputString = new StringBuilder();
StringBuilder errorString = new StringBuilder();
p.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
outputString.AppendLine("Info " + e.Data);
}
};
p.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
errorString.AppendLine("EEEE " + e.Data);
}
};