Detecting a network connection

  • Thread starter Thread starter Juan Gabriel Del Cid
  • Start date Start date
J

Juan Gabriel Del Cid

In C#, what is the best way to detect whether a network
connection is present?

Use WMI, like so:

using System;
using System.Management;
using System.Windows.Forms;

namespace Tests {
public class NetworkProbe {
public static void Main(string []args) {
ObjectQuery objectQuery = new ObjectQuery(
"select * from Win32_NetworkAdapter " +
"where NetConnectionStatus=2"); // 2 means connected

ManagementObjectSearcher searcher =
new ManagementObjectSearcher(objectQuery);

int connectedNetworks = searcher.Get().Count;

if (connectedNetworks > 0) {
Console.WriteLine("There are {0} connected network interfaces",
connectedNetworks);
} else {
Console.WriteLine("There is no network connection present");
}
}
}
}

That should do it,
-JG
 
Hello -

I'm writing an application that will be used on "sometimes connected"
devices (laptops, tablets).

In C#, what is the best way to detect whether a network connection is
present? I'd rather not rely on a network request timing out - the detection
process must be very quick.

Thanks in advance,
Scott
 
Scott said:
Hello -

I'm writing an application that will be used on "sometimes connected"
devices (laptops, tablets).

In C#, what is the best way to detect whether a network connection is
present? I'd rather not rely on a network request timing out - the
detection process must be very quick.

Try System.Windows.Forms.SystemInformation.Network. It is, of course,
true when a network connection is present.


HTH,

Christina Androne
 
Check the ispluggedin method of the networkconnection class.



Scott Leonard said:
Hello -

I'm writing an application that will be used on "sometimes connected"
devices (laptops, tablets).

In C#, what is the best way to detect whether a network connection is
present? I'd rather not rely on a network request timing out - the detection
process must be very quick.

Thanks in advance,
Scott




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
Back
Top