|
-
Jun 29th, 2009, 07:47 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Checking for the active network connection (LAN/Wireless/Modem)
Hello,
VS 2008 SP1
I am using the code below to test whether the user is connected using either a wireless or LAN connection. i.e. that the cable is plugged in, or the wireless is switch off. The code works fine for this. However, if you can spot any potential problems with this or you know of a better way I would be interested to learn more.
However, the client would like us to check if the user is connected using a modem as well. As you can see from my source code I am looking for connection that start with either "Local Area Connection" or "Wireless Network Connection". This is ok.
However, the problem is that the modem name could be anything, as when the user sets up their modem connection using the 'new connection wizard' they can call this anything. So if my switch statement I don't know what to look for.
Any suggestions would be most helpfull,
Code:
' Checks if Network is either connected by LAN or Wireless
Public Function IsNetworkConnected() As Boolean
Dim networkCards As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim connected As Boolean = False
' Loop through to find the one we want to check for connectivity.
' Connection can have different numbers appended so check that the
' network connections start with the conditions checked below.
For Each nc As NetworkInterface In networkCards
' Check LAN
If nc.Name.StartsWith("Local Area Connection") Then
If nc.OperationalStatus = OperationalStatus.Up Then
connected = True
End If
End If
' Check for Wireless
If nc.Name.StartsWith("Wireless Network Connection") Then
If nc.OperationalStatus = OperationalStatus.Up Then
connected = True
End If
End If
Next
Return connected
End Function
-
Jun 29th, 2009, 07:53 PM
#2
Re: Checking for the active network connection (LAN/Wireless/Modem)
to be honest, so could "Local Area Connection" or "Wireless Network Connection". These aren't always static.
here's what I do. Theoretically, if you could ping a website address or remote IP, then this would translate to being connected to the Internet. So basically:
vb Code:
Private Function IsDestinationReachable(ByVal hostnameOrAddress As String) As Boolean Dim reachable As Boolean = False Try reachable = My.Computer.Network.IsAvailable AndAlso _ My.Computer.Network.Ping(hostnameOrAddress, 10000) Catch pingException As System.Net.NetworkInformation.PingException Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException ' Fail silently and return false End Try Return reachable End Function
and call it like:
vb Code:
If IsDestinationReachable(yourIPorRemoteIP) Then 'You could use an NTP server IP here or the client's Gateway or make this a user input (textbox) that they supply at run time 'Connected Else 'Not Connected End if
-
Jun 29th, 2009, 09:50 PM
#3
Thread Starter
Frenzied Member
Re: Checking for the active network connection (LAN/Wireless/Modem)
Hello,
Thanks for your effort and code sample. However, finding if I am connected to the Internet is not what I was looking for.
I was wondering how I could modify the source code to detect if I was connected to the Internet by either LAN, Wireless, or Modem? This is what the client needs.
So far my code detects if I am connected using either a LAN or wireless, but I was having trouble with the modem, as many people name their modem connection different.
Many thanks for any suggestions,
-
Jun 29th, 2009, 09:53 PM
#4
Fanatic Member
Re: Checking for the active network connection (LAN/Wireless/Modem)
Hey steve_rm, check out this link, it uses API, but I'm sure it can help you out, at least with getting the LAN, modem and proxy connection.
-
Jun 29th, 2009, 09:59 PM
#5
Re: Checking for the active network connection (LAN/Wireless/Modem)
Can you not just check My.Computer.Network.IsAvailable property?
-
Jun 29th, 2009, 10:11 PM
#6
Fanatic Member
Re: Checking for the active network connection (LAN/Wireless/Modem)
He can but he wants the connection type (right?)... So that would check if he IS connected...
-
Jun 29th, 2009, 10:21 PM
#7
Re: Checking for the active network connection (LAN/Wireless/Modem)
ahh ok my bad. didn't know how necessary that requirement was (distinguishing the connection type)
best of luck.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|