3

Question says it all really, is there a C# answer to VB.NET's My.Computer.Network.Ping ?

Cheers!

Svetlozar Angelov
  • 21,214
  • 6
  • 62
  • 67
JoeBeez
  • 311
  • 2
  • 6
  • 15
  • 1
    This has been asked before: http://stackoverflow.com/questions/142614/traceroute-and-ping-in-c – Sivvy Nov 25 '09 at 14:54
  • @Sivvy: I have voted to close it after seeing your comments & the original question. Not sure, if it is really required to close it. The way "related" questions are shown depends on the text of the title (which doesn't speak of Ping separated by space). – shahkalpesh Nov 25 '09 at 14:59

5 Answers5

6

See http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

    // 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);
        }
    }
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
4

Why not just add a reference to the Microsoft.VisualBasic.dll? In Solution Explorer, right click the solution and select Add Reference. When the References dialog appears find and select the Microsoft.VisualBasic.dll, click OK.

Microsoft.VisualBasic.Devices.Network net = new Network();
bool success = net.Ping("www.google.com");
Phaedrus
  • 8,351
  • 26
  • 28
  • Wow, a C# programmer adding a reference to the native VB dll. That just sounds *wrong*. – Josh Stodola Nov 25 '09 at 15:02
  • 5
    This is just C# pedantry. What's wrong about it? It's a perfectly acceptable solution. There is nothing 'wrong' with a C# programmer who uses all of the tools available. If you can provide me with an explanation of why this is 'wrong' and why all the down votes i would love to hear it. Apparently it doesn't make Scott Hanselman cry, here's a quote "There's good stuff in Microsoft.VisualBasic.dll, and just because it isn't System.Something doesn't mean you shouldn't reference it with abandon. Go nuts." - http://www.hanselman.com/blog/CommentView.aspx?guid=d2f676ea-025b-4fd6-ae79-80b04a34f24c – Phaedrus Nov 25 '09 at 15:44
  • `Why not just add a reference to the Microsoft.VisualBasic.dll?` in 2023 this kinda makes me wince a bit. I could see this thinking in 2009 though :) – ScottN Jun 16 '23 at 21:16
4
public bool Ping(host)
{
    System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
    if (p.Send(host, 500).Status == System.Net.NetworkInformation.IPStatus.Success) {
        return true;
    } else {
        return false;
    }
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
sven
  • 41
  • 1
3

There is a Ping class in the System.Net.NetworkInformation namespace.

Dan
  • 17,375
  • 3
  • 36
  • 39
1

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

EDIT: Alternatively, you could add reference to Microsoft.VisualBasic.dll which resides in GAC & use it from c#, than to write code by yourself ;)

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88