tag:blogger.com,1999:blog-6698224.post-1087312287664543652004-06-15T10:10:00.000-05:002004-08-11T10:49:17.503-05:00How to Ping in C# using System.Management <span style="font-family:trebuchet ms;font-size:85%;">If you are using Windows XP, Windows Server 2003 or above perhaps you might want to avoid using unsupported code for emulating the Ping.exe utility that comes with Windows. Microsoft has introduced the new </span><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp"><span style="font-family:trebuchet ms;font-size:85%;">Win32_PingStatus</span></a><span style="font-family:trebuchet ms;font-size:85%;"> WMI class. You can use the System.Management classes to access an Win32_PingStatus object directly or generate a wrapper by using the </span><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrfmanagementstronglytypedclassgeneratormgmtclassgenexe.asp"><span style="font-family:trebuchet ms;font-size:85%;">Management Strongly Typed Class Generator Tool</span></a><span style="font-family:trebuchet ms;"><span style="font-size:85%;">. <br />Following the second option, open the Visual Studio .NET Command Prompt and type: <br /></span> <br /></span><span style="font-family:trebuchet ms;"><span style="font-size:85%;"><span style="font-family:arial;">C:\>mgmtclassgen.exe Win32_PingStatus</span> <br /></spam> <br /></span></span><span style="font-family:trebuchet ms;"><span style="font-size:85%;">This will generate the file "PingStatus.CS". Create a new C# project and add the "PingStatus.CS" file. Use the PingStatus class as follows:</span></span> <br /> <br /><span style="font-family:arial;font-size:78%;">using System; <br />using ROOT.CIMV2.Win32; <br />sealed class MyPing { <br />static void Main(string[] args) <br />{ <br />if(args.Length == 0) <br />{ <br />Console.Error.WriteLine("Usage: myping"); <br />return; <br />} <br />PingStatus ping = Ping(args[0]); <br />// Check if we got an answer <br />if(ping.PrimaryAddressResolutionStatus == 0) <br />{ <br />Console.WriteLine("Resolved: {0}", ping.ProtocolAddress); <br />} <br />else <br />{ <br />Console.Error.WriteLine("Error: '{0}' not resolved.", ping.Address); </span><span style="font-family:arial;font-size:78%;"> <br />} <br />} <br />static PingStatus Ping(string address) <br />{ <br />string condition = string.Format("Address='{0}'", address); <br />foreach(PingStatus ping in PingStatus.GetInstances(condition)) <br />{ <br />return ping; <br />} <br />return null; <br />}}</span> <br /> <br /><span style="font-family:trebuchet ms;"><span style="font-size:85%;">The C# code above pings an address (IP or host name) and checks if it got an answer. Please check the <strong>Win32_PingStatus</strong> WMI class documentation on the </span></span> <br /><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp"><span style="font-family:trebuchet ms;font-size:85%;">MSDN</span></a><span style="font-family:trebuchet ms;font-size:85%;"> for property descriptions and status codes. <br /></spam> <br /></span> <br /> <br />Danielhttp://www.blogger.com/profile/17636031092965440114noreply@blogger.com