Home
Manage Your Code
Snippet: Test whether a computer is alive or not. (C#)
Title: Test whether a computer is alive or not. Language: C#
Description: Test whether a computer is alive or not. do it by ping. Views: 146
Author: Iwillloveit Iwillloveit Date Added: 5/7/2008
Copy Code  
1//Check whether the node is alive or not. If alive, return the IP.
2        protected static string CheckNodeConnectivity (string hostName)
3        {
4            bool nodeIsAlive=false;
5            string nodeIP = string.Empty;
6            using (Ping ping=new Ping() )
7            {
8                try
9                {
10                    PingReply reply = ping.Send(hostName, 600); //600 ms
11                    if (reply.Status ==IPStatus .Success )
12                    {
13                        nodeIsAlive = true;
14                        nodeIP = reply.Address.ToString();
15                    }
16                }
17                catch (Exception err)
18                {
19                   // Console.WriteLine("Error has occurred in method CheckNodeConnectivity: " +err.Message );
20                }
21
22
23            }
24            return nodeIP;
25        }
Usage
Need to add these:

using System.Net;
using System.Net.NetworkInformation;