0

I have made an application in C# visual studio 2010. In this application I am sending ping command to cmd and the receiving the output of the cmd in a RichTextBox. Here is my code:-

void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        string newLine = e.Data.Trim() + Environment.NewLine;
        MethodInvoker append = () => txtOutput.Text += newLine;
        txtOutput.BeginInvoke(append);
    }
}
private void btnPing_Click(object sender, EventArgs e)
{
    string command = "/c ping " + txtPing.Text;

    ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);

    Process proc = new Process();
    proc.StartInfo = procStartInfo;
    proc.Start();

    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
    proc.Start();
    proc.BeginOutputReadLine();
    proc.WaitForExit();
}

my code is working great but the issue is cmd is also getting popup when I click on start button. And I want to hide this cmd as I want to show output only in RichTextBox. So, my question is, how can I hide cmd in my application. here is the screen shot of my problem.enter image description here

Tabish Saifullah
  • 570
  • 1
  • 5
  • 25

2 Answers2

0

Try this:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

This worked for me.

SomeUser
  • 390
  • 8
  • 23
0

Try adding these lines. This worked for me.

 procStartInfo.CreateNoWindow = true;
 procStartInfo.UseShellExecute = false;
 procStartInfo.RedirectStandardOutput = true;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jamil
  • 330
  • 1
  • 6
  • 25