Hello,
VS 2008
I am developing an application that has to detect whether a client has a network connection. i.e. LAN cable plugged in or wireless switched on. I have been using the code below.
I am using the NetworkAvailabilitychangedEvent to fire an event when their wireless is turned off or the cable has been pulled out. However, this only works if the user has only 3 connections present (LAN, Wireless, and loopbacks).
Microsoft: "The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface".
However, some client has more than 3 connections. One client had a bluetooth connection and someone else had some VMWare connections. On these client it failed to fire the event.
Is there anyway I can ignore all these connections I am not interested in listening out for, and just listen on the LAN and Wireless?
Many thanks for any advice,
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(OnNetworkChangedEvent);
}
private void OnNetworkChangedEvent(object sender, NetworkAvailabilityEventArgs e)
{
bool available = e.IsAvailable;
NetworkInterface[] networkConnections = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in networkConnections)
{
if (ni.Name == "Local Area Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("LAN disconnected: " + ni.Description);
}
}
else if (ni.Name == "Wireless Network Connection")
{
if (ni.OperationalStatus == OperationalStatus.Down)
{
Console.WriteLine("Wireless disconnected: " + ni.Description);
}
}
}
}