25

I am attempting to ping a local computer from my C# program. To accomplish this, I'm using the following code.

System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = @"C:\windows\system32\cmd.exe";
proc.Arguments = @"""ping 10.2.2.125""";
System.Diagnostics.Process.Start(proc);

This opens a command-line window, but ping is not invoked. What is the reason?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124

6 Answers6

45

You need to include the "/c" argument to tell cmd.exe what you mean it to do:

proc.Arguments = "/c ping 10.2.2.125";

(You could call ping.exe directly of course. There are times when that's appropriate, and times when it's easier to call cmd.)

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • @whihathac: "it doesn't work" doesn't give much information. What happens for you? – Jon Skeet Jul 15 '14 at 22:43
  • hi how can I store the process output in string to display the output in my own application without opening the console window – Arijit Mukherjee Nov 10 '14 at 05:32
  • 1
    @ArijitMukherjee: That sounds like an entirely different question to me - so I suggest you ask it in a new post (after searching for answers here, of course). – Jon Skeet Nov 10 '14 at 06:44
11
public void ExecuteCommand(String command)
{
   Process p = new Process();
   ProcessStartInfo startInfo = new ProcessStartInfo();
   startInfo.FileName = "cmd.exe";
   startInfo.Arguments = @"/c " + command; // cmd.exe spesific implementation
   p.StartInfo = startInfo;
   p.Start();
}

Usage: ExecuteCommand(@"ping google.com -t");

Community
  • 1
  • 1
DeathRs
  • 1,060
  • 17
  • 22
8
cmd /C 

or

cmd /K

Probably /C because /K does not terminate right away

Alain Pannetier
  • 9,008
  • 3
  • 40
  • 46
6

You could just use the System.Net.NetworkInformation.Ping class.

    public static int GetPing(string ip, int timeout)
    {
        int p = -1;
        using (Ping ping = new Ping())
        {
                PingReply reply = ping.Send(_ip, timeout);
                if (reply != null)
                    if (reply.Status == IPStatus.Success)
                        p = Convert.ToInt32(reply.RoundtripTime);
        }
        return p;
    }
JYelton
  • 34,250
  • 25
  • 123
  • 184
  • Good alternative but for all you know he wants to display the output to the user or on a console window. – Alex Essilfie Feb 23 '11 at 08:39
  • 1
    This answer was based on the comments on the question wherein Matt suggested calling Ping directly, and the OP asked how to go about doing it. – JYelton Feb 23 '11 at 16:23
2

To call the ping command directly, do as you have in your question, but substitute cmd.exe with ping.exe:

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\windows\system32\ping.exe";
proc.Arguments = @"10.2.2.125";
Process.Start(proc);
Matt Ellen
  • 10,460
  • 4
  • 67
  • 85
0

If you are looking for a more advanced way of working with external command line executables, you can try CliWrap. For example, you can use it to easily stream ping results in real-time, as they're written to the console by the underlying process:

var cmd = Cli.Wrap("ping").WithArguments("stackoverflow.com");
await foreach (var evt in cmd.ListenAsync())
{
     if (evt is StandardOutputCommandEvent stdOut)
     {
         Console.WriteLine(stdOut.Text);
     }
}
Tyrrrz
  • 2,195
  • 1
  • 19
  • 22