0

I have a process that I need hidden, I have tried the following lines of code to make it hidden:

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

The first line just simply does not make it non-visible and the second throws the following error:

"{"StandardOut has not been redirected or the process hasn't started yet."}

Also I need to have the output redirected to a richtextbox and the clipboard, so I cannot set redirectstandardoutput to false. Here is my function for creating the process.

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.");                
        }

Thanks

ploxiblox
  • 67
  • 7

1 Answers1

1

Remove the following line:

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Keep these two lines:

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

WindowStyle only applies to native Windows GUI applications.

Derek W
  • 9,708
  • 5
  • 58
  • 67
  • Thank you that worked. I swear I tried that earlier... I must have forgotten a line... Thanks Again! – ploxiblox Mar 18 '14 at 20:27
  • Not a problem. If this is a long running process - you might want to look into the `OutputDataReceived` Event of the `Process` class. – Derek W Mar 18 '14 at 20:32
  • I will look into that, although it just pings once and exits. – ploxiblox Mar 18 '14 at 20:37
  • It's probably not necessary for this situation then, but it's always nice to have another tool in your back pocket. Rigging that event up with a `BackgroundWorker` can yield some nice results. – Derek W Mar 18 '14 at 20:44