Question says it all really, is there a C# answer to VB.NET's My.Computer.Network.Ping
?
Cheers!
Question says it all really, is there a C# answer to VB.NET's My.Computer.Network.Ping
?
Cheers!
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);
}
}
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");
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;
}
}
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 ;)