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

Re: How to: Determine if you have a Network Connection??? by John

John
Wed Sep 10 13:53:19 CDT 2003

"Steven Van Dyke" <svandyke@rft.com> wrote in message
news:utYo0o8dDHA.2640@TK2MSFTNGP09.phx.gbl...
> 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?
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com



Re: How to: Determine if you have a Network Connection??? by Steven

Steven
Wed Sep 10 15:35:32 CDT 2003

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

"John Saunders" <john.saunders@surfcontrol.com> wrote in message
news:ebfDlz8dDHA.2524@TK2MSFTNGP09.phx.gbl...
> "Steven Van Dyke" <svandyke@rft.com> wrote in message
> news:utYo0o8dDHA.2640@TK2MSFTNGP09.phx.gbl...
> > 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?
> --
> John Saunders
> Internet Engineer
> john.saunders@surfcontrol.com
>
>



Re: How to: Determine if you have a Network Connection??? by John

John
Wed Sep 10 15:42:05 CDT 2003

"Steven Van Dyke" <svandyke@rft.com> wrote in message
news:uz62Vs9dDHA.576@tk2msftngp13.phx.gbl...
> 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.

--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com



Re: How to: Determine if you have a Network Connection??? by Steven

Steven
Wed Sep 10 15:57:31 CDT 2003

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





"John Saunders" <john.saunders@surfcontrol.com> wrote in message
news:%234BkXw9dDHA.1944@TK2MSFTNGP12.phx.gbl...
> "Steven Van Dyke" <svandyke@rft.com> wrote in message
> news:uz62Vs9dDHA.576@tk2msftngp13.phx.gbl...
> > 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.
>
> --
> John Saunders
> Internet Engineer
> john.saunders@surfcontrol.com
>
>



Re: How to: Determine if you have a Network Connection??? by John

John
Wed Sep 10 16:05:31 CDT 2003

"Steven Van Dyke" <svandyke@rft.com> wrote in message
news:%23rU%23o49dDHA.2328@TK2MSFTNGP12.phx.gbl...
> 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?
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com



Re: How to: Determine if you have a Network Connection??? by Andrea

Andrea
Wed Sep 10 16:02:24 CDT 2003

> 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
--
AZ



Re: How to: Determine if you have a Network Connection??? by Steven

Steven
Wed Sep 10 17:34:41 CDT 2003

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" <andrew@aspitalia.com> wrote in message
news:bjo4e0$l8of0$3@ID-192116.news.uni-berlin.de...
> > 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
> --
> AZ
>
>



Re: How to: Determine if you have a Network Connection??? by Steven

Steven
Wed Sep 10 17:39:20 CDT 2003

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" <andrew@aspitalia.com> wrote in message
news:bjo4e0$l8of0$3@ID-192116.news.uni-berlin.de...
> > 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
> --
> AZ
>
>



RE: How to: Determine if you have a Network Connection??? by quansun

quansun
Thu Sep 11 08:01:07 CDT 2003

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.