Need to know the LAN connection status

G

Guest

I'm designing an application that will be run on LAN-connected desktop PCs,
and will also be run on laptops that may or may not be disconnected from the
LAN. The application also needs to continue functioning if the network goes
down.

Looking for code to determine if a LAN connection is present first led me to
Sysytem.Windows.Forms.SystemInformation.Network, but that appears to only
tell you if a valid NIC is in the machine.... worthless.

I next checked out the following API calls: IsConnected, GetConnectedState,
IsNetworkAlive and InternetGetConnectedState. All of those appear to only
tell you the LAN status at boot-up time. That won't work if users unplug, or
if the network goes down.

Windows has the ability to know when a LAN connection is present, because
the little icon in the taskbar shows the correct status. How can I get VB.Net
to be that aware? I'd be happy to query whatever data drives the little
taskbar icon, if I have to...

Thanks,
Matt
 
S

Shiva

Using WMI is one option:

ManagementObjectSearcher ms = new ManagementObjectSearcher ("SELECT
NetConnectionStatus FROM Win32_NetworkAdapter");
foreach (ManagementObject mo in ms.Get())
{
if (mo["NetConnectionStatus"] != null)
{
Console.WriteLine(mo["NetConnectionStatus"]);
}
}

Please note that this works from Win XP onwards.

I'm designing an application that will be run on LAN-connected desktop PCs,
and will also be run on laptops that may or may not be disconnected from the
LAN. The application also needs to continue functioning if the network goes
down.

Looking for code to determine if a LAN connection is present first led me to
Sysytem.Windows.Forms.SystemInformation.Network, but that appears to only
tell you if a valid NIC is in the machine.... worthless.

I next checked out the following API calls: IsConnected, GetConnectedState,
IsNetworkAlive and InternetGetConnectedState. All of those appear to only
tell you the LAN status at boot-up time. That won't work if users unplug, or
if the network goes down.

Windows has the ability to know when a LAN connection is present, because
the little icon in the taskbar shows the correct status. How can I get
VB.Net
to be that aware? I'd be happy to query whatever data drives the little
taskbar icon, if I have to...

Thanks,
Matt
 
G

Guest

One possibility is to create two flows in the application, and use exceptions
to control it. I use that for one app I have written that uses webservices...
if I get an exception indicating the webservice can't be reached, catch it
and perform alternate tasks in the catch statement...

kind regards
henrik
 
G

Guest

Shiva,

Thanks for the prompt response. Unfortunately, I'm working on Win2000 PCs
here, so this won't help me currently.
 
G

Guest

Henrik,
Thanks for the prompt response. I was afraid I might have to let it fall to
an exception. I wanted to avoid the delay and proactively get the status, as
opposed to waiting for an exception. If I was on XP, I would use the great
suggestion Shiva had and use WMI. As it is, I can use
InternetGetConnectedState for the people who are booting up unplugged, and
let an exception handle the situation where the LAN goes down. Thanks for
your help.

Henrik Nordgren said:
One possibility is to create two flows in the application, and use exceptions
to control it. I use that for one app I have written that uses webservices...
if I get an exception indicating the webservice can't be reached, catch it
and perform alternate tasks in the catch statement...

kind regards
henrik

Shiva said:
Using WMI is one option:

ManagementObjectSearcher ms = new ManagementObjectSearcher ("SELECT
NetConnectionStatus FROM Win32_NetworkAdapter");
foreach (ManagementObject mo in ms.Get())
{
if (mo["NetConnectionStatus"] != null)
{
Console.WriteLine(mo["NetConnectionStatus"]);
}
}

Please note that this works from Win XP onwards.

I'm designing an application that will be run on LAN-connected desktop PCs,
and will also be run on laptops that may or may not be disconnected from the
LAN. The application also needs to continue functioning if the network goes
down.

Looking for code to determine if a LAN connection is present first led me to
Sysytem.Windows.Forms.SystemInformation.Network, but that appears to only
tell you if a valid NIC is in the machine.... worthless.

I next checked out the following API calls: IsConnected, GetConnectedState,
IsNetworkAlive and InternetGetConnectedState. All of those appear to only
tell you the LAN status at boot-up time. That won't work if users unplug, or
if the network goes down.

Windows has the ability to know when a LAN connection is present, because
the little icon in the taskbar shows the correct status. How can I get
VB.Net
to be that aware? I'd be happy to query whatever data drives the little
taskbar icon, if I have to...

Thanks,
Matt
 
G

Guest

I hope this does not come to late:
I have the some problem of detecting the NetConnectionStatus inWin2000.
Now I have developed following solution:
First just query the WMI for the required networkadapter, youshould make object containing the Networkadapter andNetworkadapterConfiguration.
In WinXp you can read the ITem "NetConnectionStatus" as known.
In case of Win2000 or Win2003 (you may detect hich OS you areusing be the OperatingSystem-Object, such code is in yourMSDN-Library (getVersion-function that corresponds the OS to theMajor-/Minor versionnumbers) you can examine the Item"IPAdress". In networkadapters (Adapters with a MACAdress orIPEnabled=True) this Item contains the static or dynamikIP-Adress like "120.123.34.0" if there is a physical link. Ifthe Adapter is disconnected than the networkcard sets its IP to"0.0.0.0" even if in the windows-registry still "120.123.34.0"is valid.
So if the IPAdress you get from the WMI is "0.0.0.0" you knowthere is no link!
I think you never can set the IP-adress of an adapter manualy orprogramatically to 0.0.0.0. Windows won't let you!
Please email me if this helped you (e-mail address removed)
I'm designing an application that will be run on LAN-connecteddesktop PCs,
and will also be run on laptops that may or may not bedisconnected from the
LAN. The application also needs to continue functioning if thenetwork goes
down.

Looking for code to determine if a LAN connection is presentfirst led me to
Sysytem.Windows.Forms.SystemInformation.Network, but thatappears to only
tell you if a valid NIC is in the machine.... worthless.

I next checked out the following API calls: IsConnected,GetConnectedState,
IsNetworkAlive and InternetGetConnectedState. All of thoseappear to only
tell you the LAN status at boot-up time. That won't work ifusers unplug, or
if the network goes down.

Windows has the ability to know when a LAN connection ispresent, because
the little icon in the taskbar shows the correct status. Howcan I get VB.Net
to be that aware? I'd be happy to query whatever data drivesthe little
taskbar icon, if I have to...

Thanks,
Matt
User submitted from AEWNET (http://www.aewnet.com/)
 

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