How to: Determine if you have a Network Connection???

  • Thread starter Thread starter Steven Van Dyke
  • Start date Start date
S

Steven Van Dyke

Hi

I need a code snippet to determine if my computer is connected to a network
or not.

There's probably a System.Net function for it, but I cannot find it.

Thanks,

Steve
 
Steven Van Dyke said:
Hi

I need a code snippet to determine if my computer is connected to a network
or not.

There's probably a System.Net function for it, but I cannot find it.

You probably want to get a bit more specific. For instance, do you care
_which_ network it's connected to? Would a home network with just one
printer on it be as useful to you as being connected to the Internet?
 
This is an app for work. It needs to gain access to other computers on the
network. It does NOT need to get on the Internet. Basically, If I'm
connected to a network, I populate a combo box with all of the machine names
on the network. If I'm not connected, I just put the local machine name in
the combo box. My only issue is, how do I easily determine if I have a
connection or not?

Steve
 
Steven Van Dyke said:
This is an app for work. It needs to gain access to other computers on the
network. It does NOT need to get on the Internet. Basically, If I'm
connected to a network, I populate a combo box with all of the machine names
on the network. If I'm not connected, I just put the local machine name in
the combo box. My only issue is, how do I easily determine if I have a
connection or not?

I think I'd just try it and find out. If it fails, just put the local
machine name in the box. If it succeeds, put all of them there. This will
also take care of the case where you're connected to the network, but some
other problem prevents you from getting the list of machine names.
 
Hi,

Thanks for your response. I'm using the API call NetServerEnum. When my
network cable is plugged in, it succeeds, and returns all of my network
computer names. When the cable is disconnected, it still succeeds, returning
just 1 computer name "sv-file". I was hoping it would either fail, or return
zero entries. I didn't think it would be a good idea to say "if (count > 1)
then connected..."

I'm not sure why "sv-file" still appears in the list. Any thoughts on the
best solution?

Steve
 
Hi
I need a code snippet to determine if my computer is connected to a network
or not.

There's probably a System.Net function for it, but I cannot find it.

Thanks,

Steve

Read this document:
http://www.mentalis.org/apilist/InternetGetConnectedState.shtml

And read this example (author Cangiano):

using System ;
using System.Runtime ;
using System.Runtime.InteropServices ;

public class ConnectionState
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( int out
Description, int ReservedValue ) ;

public static bool IsConnected( ){
int Descrizione ;
return InternetGetConnectedState( out Descrizione, 0 ) ;}
}

Bye
 
Steven Van Dyke said:
Hi,

Thanks for your response. I'm using the API call NetServerEnum. When my
network cable is plugged in, it succeeds, and returns all of my network
computer names. When the cable is disconnected, it still succeeds, returning
just 1 computer name "sv-file". I was hoping it would either fail, or return
zero entries. I didn't think it would be a good idea to say "if (count > 1)
then connected..."

I'm not sure why "sv-file" still appears in the list. Any thoughts on the
best solution?

Is sv-file your computer? If not, then I have no idea. Is it the domain
controller or something? With the cable disconnected, are you able to do
"NET VIEW \\SV-FILE"? How about when it's connected?
 
Thanks for your response. I tried your code snippet. It seems to return the
same Descrizione value of 18 whether my network cable is connected or not.
So, it doesn't seem to solve my problem.

Steve


Andrea Zani said:
Hi

I need a code snippet to determine if my computer is connected to a network
or not.

There's probably a System.Net function for it, but I cannot find it.

Thanks,

Steve

Read this document:
http://www.mentalis.org/apilist/InternetGetConnectedState.shtml

And read this example (author Cangiano):

using System ;
using System.Runtime ;
using System.Runtime.InteropServices ;

public class ConnectionState
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( int out
Description, int ReservedValue ) ;

public static bool IsConnected( ){
int Descrizione ;
return InternetGetConnectedState( out Descrizione, 0 ) ;}
}

Bye
 
Hi,

I tried your solution. It seems to return the same Descrizione value of 18
whether my network card cable is connected or not. So, this doesn't help my
problem.

There must be some simple function that can determine if there is a live
network cable plugged into my network card. Any other suggestions?

Steve

Andrea Zani said:
Hi

I need a code snippet to determine if my computer is connected to a network
or not.

There's probably a System.Net function for it, but I cannot find it.

Thanks,

Steve

Read this document:
http://www.mentalis.org/apilist/InternetGetConnectedState.shtml

And read this example (author Cangiano):

using System ;
using System.Runtime ;
using System.Runtime.InteropServices ;

public class ConnectionState
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( int out
Description, int ReservedValue ) ;

public static bool IsConnected( ){
int Descrizione ;
return InternetGetConnectedState( out Descrizione, 0 ) ;}
}

Bye
 
You may consider to use WMI to achieve it. The following VBS demos how to
detect the disconnected adaptors:

=============================================
Set NetworkAdapters =
GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapter")

For Each Adapter In NetworkAdapters
If Adapter.NetConnectionStatus = 7 Then
Wscript.echo "Name : " & Adapter.Name
Wscript.echo "Status : Disconnected"
End If
Next
=============================================

Best regards,

Duke Sun
Microsoft Online Partner Support
<MCSE/MCDBA/MCSD>

Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top