Categories C#

How to ping your endpoint using C#?

C# has a new feature for you to ping your endpoint programmatically using the Ping class from System.Net.NetworkInformation library.

How to get installed System.Net.NetworkInformation?

Go to NuGet Package Manager, search “System.Net.NetWorkInformation” and install it.

Tools -> NuGet Package Manager -> Manage NuGet Package for Solution…

Or

Right click on Project in Solution Explorer and choose “Manage NuGet Packages..”

Example Code

 static void Main(string[] args)
 {
     string YourEndPoint = "www.codewithsiva.com";
     Ping pingyourendpoint = new Ping();
     PingReply ping = pingyourendpoint.Send(YourEndPoint);
  
     if (ping.Status == IPStatus.Success)
     {
         Console.WriteLine($"Ping to {YourEndPoint} was successfuly.");
         Console.WriteLine($"IP Address: {ping.Address}");
         Console.WriteLine($"Round Trip Time: {ping.RoundtripTime} milliseconds");
         Console.WriteLine($"Time to live: {ping.Options.Ttl}");
         Console.WriteLine($"Don't Fragment: {ping.Options.DontFragment}");
         Console.WriteLine($"Buffer: {ping.Buffer.Length}");
     }
     else
         Console.WriteLine($"Ping to {YourEndPoint} failed with status: {ping.Status}");
 }
C#

Output:

Written By

With over a decade of experience in .NET technologies, SQL Server, and Agile methodologies, I am a Lead Software Developer at TEKsystems, where I design, develop, and support web-based applications for a leading market research company. I hold multiple certifications in LINQ, Entity Framework, and other .NET frameworks and tools, demonstrating my proficiency and commitment to continuous learning.

My core competencies include .NET Core, User Defined Functions, Stored Procedures, MVC, Web API, jQuery, and Bootstrap. I have successfully delivered several projects, such as a dashboard for analyzing consumer behavior, a portal for managing surveys and reports, and a tool for automating data quality checks. I am passionate about creating innovative and user-friendly solutions that help clients make informed business decisions. As part of a collaborative and agile team, I contribute to improving the quality, performance, and security of web applications, as well as providing technical support and documentation.

Email: codewithsivablog@gmail.com

More From Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like