Results 1 to 7 of 7

Thread: [RESOLVED] Checking for the active network connection (LAN/Wireless/Modem)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [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
    steve

  2. #2
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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:
    1. Private Function IsDestinationReachable(ByVal hostnameOrAddress As String) As Boolean
    2.         Dim reachable As Boolean = False
    3.         Try
    4.             reachable = My.Computer.Network.IsAvailable AndAlso _
    5.                         My.Computer.Network.Ping(hostnameOrAddress, 10000)
    6.         Catch pingException As System.Net.NetworkInformation.PingException
    7.         Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException
    8.             ' Fail silently and return false
    9.         End Try
    10.         Return reachable
    11.     End Function

    and call it like:

    vb Code:
    1. 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
    2.      'Connected
    3. Else
    4.      'Not Connected
    5. End if

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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,
    steve

  4. #4
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Checking for the active network connection (LAN/Wireless/Modem)

    Can you not just check My.Computer.Network.IsAvailable property?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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...
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  7. #7
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    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
  •  



Click Here to Expand Forum to Full Width