Google
WWW Yariv Hammer's Code Site

Monday, October 24, 2005

Ping in .NET 2003

Introduction
Ping is very useful when there is a need to know if a certain computer is connected to the local computer via network. The quickest way to programmatically know if another computer is currently active and connected is by using ping.
However, there is no built-in support for Ping in .NET 2003 (Microsoft added a Ping class to Framework 2.0).

Solution
This code can be used in order to check if a remote computer is alive using ping:
---------------------------------------------------

public class Ping
{
   private const string REQUEST_TIMED_OUT = "Request timed out.";
   public static bool IsConnected(string ipAddress)
   {
      Process p = new Process();
      p.StartInfo.UseShellExecute = false;
      p.StartInfo.RedirectStandardOutput = true;
      p.StartInfo.Arguments = ipAddress;
      p.StartInfo.FileName=@"ping";
      p.StartInfo.CreateNoWindow=true;
      p.Start();
      string output = p.StandardOutput.ReadToEnd();
      p.WaitForExit();
      return AnalysePingOutput(output);
   }
   private static bool AnalysePingOutput(string output)
   {
      return !(output.IndexOf(REQUEST_TIMED_OUT)>0);
   }
}

---------------------------------------------------

0 Comments:

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.