Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# verify the ping utility

Tags:

c#

From C# console application i had open the command prompt and checked the ping utility.

string aptracommand;
aptracommand = "/C ping 10.38.2.73";
Process.Start(@"cmd",aptracommand);

Now , i need to apply a conditional statement if ping request time out then it should say "Not able to connect" and if its able to ping then need to show "Server is up"

like image 225
Sohail Avatar asked Nov 30 '25 02:11

Sohail


1 Answers

You can use Ping class for this purpose. As stated below:

using System.Net.NetworkInformation;

var ping = new Ping();
var reply = ping.Send("10.38.2.73", 60 * 1000); // 1 minute time out (in ms)
if (reply.Status == IPStatus.Success)
{
   Console.WriteLine("Server is up");
}
else
{
   Console.WriteLine("Server is down");
}

You can reduce the time out value to quickly check if server is up or down.

like image 67
Furqan Safdar Avatar answered Dec 02 '25 15:12

Furqan Safdar