【问题标题】:Stop process output from being displayed in project command window停止在项目命令窗口中显示过程输出
【发布时间】:2017-04-27 22:29:30
【问题描述】:

我正在通过 Process 类启动一个 exe,我注意到该 exe 的输出显示在我的应用程序的命令窗口中。 *注意 - 当我启动 exe 时,我确保没有打开一个窗口 - 因此,当我的应用程序运行时显示的唯一窗口是我的主应用程序 project.exe。

有没有办法阻止 exe 的输出显示在我的 project.exe 命令窗口中?这是我的代码:

Process process = new Process();
string exePath = System.IO.Path.Combine(workingDir, exeString);
process.StartInfo.FileName = exePath;
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (s, e) => Logger.LogInfo(e.Data);

process.Start();

process.BeginOutputReadLine();
process.WaitForExit();

我什至尝试将 RedirectStandardOutput 设置为 false :

process.StartInfo.RedirectStandardOutput = false;

并且输出仍然放置在命令窗口中。

【问题讨论】:

  • process.BeginOutputReadLine();进程.WaitForExit();应该在进程开始之后。即在 process.Start() 之后
  • 我的错 - 这是一个错字 - 感谢您指出这一点。我会编辑帖子。
  • @loneshark99 现在测试它 - 但是你从哪里得到 Dump() ?那不是字符串类方法。
  • 我尝试在 Linqpad 中进行测试。

标签: c# shell command-line process


【解决方案1】:

当我在我的盒子上本地尝试时,这有效。你可以通过替换 exe 路径/名称来尝试一下吗?

来自 MSDN 文档。

“当进程将文本写入其标准流时,该文本通常会显示在控制台上。通过将 RedirectStandardOutput 设置为 true 以重定向 StandardOutput 流,您可以操纵或抑制进程的输出。例如,您可以过滤文本,设置不同的格式,或者将输出写入控制台和指定的日志文件”

https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx

void Main()
{
    Process process = new Process();
    string exePath = System.IO.Path.Combine(@"C:\SourceCode\CS\DsSmokeTest\bin\Debug", "DsSmokeTest.exe");
    process.StartInfo.FileName = exePath;
    process.StartInfo.WorkingDirectory = @"C:\SourceCode\CS\DsSmokeTest\bin\Debug";
    process.StartInfo.Arguments = string.Empty;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += (s, e) => Test(e.Data);
    process.Start();
    process.BeginOutputReadLine();
    process.WaitForExit();
}

// Define other methods and classes here
public void Test(string input)
{
    input.Dump();   
}

【讨论】:

    【解决方案2】:

    只需添加以下行

    process.StartInfo.UseShellExecute = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2012-04-17
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      相关资源
      最近更新 更多