C# 隐藏进程窗口

C# 隐藏进程窗口,c#,process,hidden,processstartinfo,C#,Process,Hidden,Processstartinfo,我有一个需要隐藏的进程,我尝试了以下几行代码来隐藏它: p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.CreateNoWindow = true; 第一行只是不使其不可见,第二行抛出以下错误: “{”StandardOut尚未重定向或进程尚未启动。“} 我还需要将输出重定向到richtextbox和剪贴板,这样我就不能将redirectstandardoutput设置为false。 下面是我创建流程的函数 Pr

我有一个需要隐藏的进程,我尝试了以下几行代码来隐藏它:

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
第一行只是不使其不可见,第二行抛出以下错误:

“{”StandardOut尚未重定向或进程尚未启动。“}

我还需要将输出重定向到richtextbox和剪贴板,这样我就不能将redirectstandardoutput设置为false。 下面是我创建流程的函数

Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = pingData;
        p.Start();

        p.WaitForExit();
        string result = p.StandardOutput.ReadToEnd();

        System.Windows.Forms.Clipboard.SetText(result);
        if(p.HasExited)
        {
            richTextBox1.Text = result;
            outPut = result;
            MessageBox.Show( "Ping request has completed. \n Results have been copied to the clipboard.");                
        }

谢谢

删除以下行:

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
保持这两条线:

p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;

WindowStyle
仅适用于本机Windows GUI应用程序。

谢谢你的帮助。我发誓我之前试过了……我一定忘了一行……再次感谢!没问题。如果这是一个长时间运行的进程-你可能想查看
进程
类的
OutputDataReceived
事件。我会查看对于这一点,虽然只需ping一次就可以退出。在这种情况下可能没有必要这样做,但在您的后袋中有另一个工具总是很好的。使用
BackgroundWorker
将该事件装配起来可以产生一些不错的结果。