Test if host is online

G

Guest

I would like to find a simple way to test if a computer is online.

I found a lot of code on internet about using existing ping class...but the
code was not looking really good. There must be a simple way to do that?

Thanks!
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

if you want to know if a host is accesible a ping MAY be helpful, it may
also be filtered out so you cannot really trust on it.

You could also try to connect to a known service that host provide, in this
case you have the drawback that the service may be down but the host still
up :)


It depends of your intentions about what is the best solution.

cheers,
 
G

Guest

Well, I would like to go with a ping...but I have notice that c# doesn't
containt a easy way to do a ping. I have try to use a existing ping
application (http://www.dataidee.com/ping/ping.htm)

The problem is that you got to be a admin on the computer to use
"SocketType.Raw"

I can't belive theres nothing simple that let you know if a computer is
online...
 
G

Guest

Well, finally I have found something....

http://spaces.msn.com/members/dotnoted/PersonalSpace.aspx?_c01_blogpart=blogmgmt&_c=blogpart

private bool isMachineReachable(string hostName)

{

System.Net.IPHostEntry host = System.Net.Dns.GetHostByName(hostName);

string wqlTemplate = "SELECT StatusCode FROM Win32_PingStatus WHERE Address
= '{0}'";

System.Management.ManagementObjectSearcher query = new
ManagementObjectSearcher();

query.Query = new ObjectQuery(String.Format(wqlTemplate,
host.AddressList[0]));

query.Scope = new ManagementScope("//localhost/root/cimv2");

ManagementObjectCollection pings = query.Get();

foreach(ManagementObject ping in pings)
{

if( Convert.ToInt32(ping.GetPropertyValue("StatusCode")) == 0)
return true;
}

return false;
}

If I have a problem with it....I will give youi my feedback on it...
 
W

Willy Denoyette [MVP]

No need to get the IP address from the hostname using GetHostByName, the
address property takes a host name or an address.

SELECT StatusCode FROM Win32_PingStatus WHERE Address='MyHostname'


Willy.

Marc-André said:
Well, finally I have found something....

http://spaces.msn.com/members/dotnoted/PersonalSpace.aspx?_c01_blogpart=blogmgmt&_c=blogpart

private bool isMachineReachable(string hostName)

{

System.Net.IPHostEntry host = System.Net.Dns.GetHostByName(hostName);

string wqlTemplate = "SELECT StatusCode FROM Win32_PingStatus WHERE
Address
= '{0}'";

System.Management.ManagementObjectSearcher query = new
ManagementObjectSearcher();

query.Query = new ObjectQuery(String.Format(wqlTemplate,
host.AddressList[0]));

query.Scope = new ManagementScope("//localhost/root/cimv2");

ManagementObjectCollection pings = query.Get();

foreach(ManagementObject ping in pings)
{

if( Convert.ToInt32(ping.GetPropertyValue("StatusCode")) == 0)
return true;
}

return false;
}

If I have a problem with it....I will give youi my feedback on it...

Marc-André said:
I would like to find a simple way to test if a computer is online.

I found a lot of code on internet about using existing ping class...but
the
code was not looking really good. There must be a simple way to do that?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top