user1482489
user1482489

Reputation: 25

how to Ping ip address with parameter(-n,-t etc) in c#

I am developing a windows form application. I have a requirement, need ping the system ip address with parameters. like ping IP Address -n 1.

I am Unable to pass the parameter using ping.send function.

Any one can help me.

Upvotes: 0

Views: 1609

Answers (2)

Karthik Nishanth
Karthik Nishanth

Reputation: 2010

use Ping class. This provides a more organized approach

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace Examples.System.Net.NetworkInformation.PingTest
{
    public class PingExample
    {
        // args[0] can be an IPaddress or host name. 
        public static void Main (string[] args)
        {
            Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();

            // Use the default Ttl value which is 128, 
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted. 
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
        }
    }
}

Upvotes: 1

Gargoyle
Gargoyle

Reputation: 67

You can spam a cmd process and get its output.

ProcessStartInfo processInfo = new ProcessStartInfo("cmd");
processInfo.RedirectStandardInput = true;
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
Process process = Process.Start(processInfo);
process.StandardInput.WriteLine("ping 127.0.0.1 -n 1 -i 10");
process.StandardInput.Close();
string answer = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close()
Console.WriteLine(answer);            

Upvotes: 0

Related Questions