【问题标题】:Hide process window隐藏进程窗口
【发布时间】:2014-03-18 19:59:05
【问题描述】:

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

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;

第一行只是简单地使它不可见,第二行抛出以下错误:

"{"StandardOut 尚未重定向或进程尚未开始。"}

我还需要将输出重定向到富文本框和剪贴板,所以我不能将 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.");                
        }

谢谢

【问题讨论】:

    标签: c# process hidden processstartinfo


    【解决方案1】:

    删除以下行:

    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    

    保留这两行:

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

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

    【讨论】:

    • 谢谢你的工作。我发誓我之前尝试过...我一定忘记了一行...再次感谢!
    • 没问题。如果这是一个长期运行的过程 - 您可能需要查看 Process 类的 OutputDataReceived 事件。
    • 我会调查一下,虽然它只是 ping 一次然后退出。
    • 在这种情况下可能没有必要,但是在你的后兜里有另一个工具总是很好的。使用BackgroundWorker 操纵该事件可以产生一些不错的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2010-11-11
    • 1970-01-01
    相关资源
    最近更新 更多