Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting network state (connected - disconnected) in C#

I am in need of a piece of code that can detect if a network connection is connected or disconnected. The connected state would mean a cable was plugged into the Ethernet connection. A disconnected state would mean there is not cable connected.

I can't use the WMI interface due to the fact that I'm running on Windows CE. I don't mind invoking the Win32 API but remember that I'm using Windows CE and running on the Compact Framework.

like image 970
Bobby Cannon Avatar asked Jun 01 '09 18:06

Bobby Cannon


2 Answers

The easiest way is to use OpenNETCF's SDF and look at the OpenNETCF.Net.NetworkInformation.NetworkInterfaceWatcher class, which will raise events when NDIS sends out notifications (like MEDIA_CONNECT and MEDIA_DISCONNECT).

You can do the same work without the SDF, of course. It involves onening the NDIS driver directly and calling IOCTL_NDISUIO_REQUEST_NOTIFICATION with a P2P message queue handle. It's not overly difficult, but there's a lot you have to get right for it to work and not leak.

like image 96
ctacke Avatar answered Oct 15 '22 17:10

ctacke


Check out this MSDN article:

Testing for and Responding to Network Connections in the .NET Compact Framework

like image 2
TWA Avatar answered Oct 15 '22 15:10

TWA