4

I need my application to check for internet connectivity on my user's computer. If there is, an image is displayed and if there isn't, a different image is displayed. Here's the code I used to get this to work:

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

    If NetworkInformation.NetworkInterface.GetIsNetworkAvailable Then
        Dim bi1 As New BitmapImage
        bi1.BeginInit()
        bi1.UriSource = New Uri("Images\greenbar.png", UriKind.Relative)
        bi1.EndInit()
        Image2.Source = bi1

    Else
        Dim bi2 As New BitmapImage
        bi2.BeginInit()
        bi2.UriSource = New Uri("Images\redbar.png", UriKind.Relative)
        bi2.EndInit()
        Image2.Source = bi2
        MessageBox.Show("INTERNET CONNECTION NOT DETECTED")
        MessageBox.Show("You must be connected to the internet to use some aspects of this application.")
        MessageBox.Show("Please re-establish connection to the Internet and try again, thank you.")
        Me.Close()

    End If
End Sub

I decided to test this on my own computer by changing my default gateway (thereby making it seem as if I lost connection). But I realized that the code still showed that I was connected. So I'm thinking that it's only checking for connectivity of the interface - which in this case, is my connection to the router (which is true, I was connected to the router).

So, the question: How do I check that the user's PC is actually connected to the internet? I read the article What is the best way to check for Internet connectivity using .NET? but it's in C# and I don't understand that.

Community
  • 1
  • 1
Kismet Agbasi
  • 537
  • 2
  • 7
  • 27
  • 1
    @CodyGray. I appreciate your comment, but I am now learning programming. I am not a pro, such an insult is undeserving. Don't you think? – Kismet Agbasi Jan 10 '12 at 08:39
  • Not really, no. C# and VB.NET are not all that different. If you're learning one, you should be able to figure out the other one. This wasn't complicated code to translate, and a simple Google search would have turned up one of the many automated translators. If an automatic translator can do it, surely you should be able to do it with a bit of thought and effort. I don't think someone should be encouraged to be helpless whether they're just learning or have been programming 25+ years. – Cody Gray Jan 10 '12 at 08:42

6 Answers6

6

You can use this tool to translate C# to VB.NET or vice-versa:

Public Shared Function CheckForInternetConnection() As Boolean
    Try
        Using client = New WebClient()
            Using stream = client.OpenRead("http://www.google.com")
                Return True
            End Using
        End Using
    Catch
        Return False
    End Try
End Function

By the way, the NetworkInterface.GetIsNetworkAvailable method you've used checks whether any network connection is available or not - not Internet Connectivity.

A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.

Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
  • Thanks for you response. I'll try it and provide feedback shortly. – Kismet Agbasi Jan 10 '12 at 08:40
  • I tried to use the code you suggested, but i got an error telling me that "Statement cannot appear within a method". I'm assuming this means that I'll have to create separate class file and call it in my method? Please forgive the questions, I'm just trying to learn. I still want to be able to display the images based on the RETURN value, so please help me incorporate your code into mine. Thanks. – Kismet Agbasi Jan 10 '12 at 08:56
  • @Kismet: Yes, the above code is a separate method that you should nest into it's own class(for example called `NetworkUtilities`). Then you could call `If NetworkUtilities.CheckForInternetConnection() Then` instead of your `If NetworkInformation.NetworkInterface.GetIsNetworkAvailable Then`. – Tim Schmelter Jan 10 '12 at 09:06
  • Thank you. I've posted what I did below, slightly different from yours but that shouldn't be a problem right? – Kismet Agbasi Jan 10 '12 at 10:17
3

Or use this code

If My.Computer.Network.IsAvailable Then
    MsgBox("Computer is connected.")
Else
    MsgBox("Computer is not connected.")
End If
Jk1
  • 10,743
  • 9
  • 51
  • 63
1
If My.Computer.Network.Ping("www.Google.com") Then
...
End If
0

You could use this, which should help you out for VB & C# versions:

Public Function IsConnectionAvailable() As Boolean
    ' Returns True if connection is available
    ' Replace www.yoursite.com with a site that
    ' is guaranteed to be online - perhaps your
    ' corporate site, or microsoft.com
    Dim objUrl As New System.Uri("http://www.google.com/")
    ' Setup WebRequest
    Dim objWebReq As System.Net.WebRequest
    objWebReq = System.Net.WebRequest.Create(objUrl)
    objWebReq.Proxy = Nothing
    Dim objResp As System.Net.WebResponse
    Try
        ' Attempt to get response and return True
        objResp = objWebReq.GetResponse
        objResp.Close()
        objWebReq = Nothing
        Return True
    Catch ex As Exception
        ' Error, exit and return False
        objResp.Close()
        objWebReq = Nothing
        Return False
    End Try
End Function
J0e3gan
  • 8,570
  • 9
  • 52
  • 78
rubin
  • 5
0
Public Function IsConnectionAvailable() As Boolean
    ' Returns True if connection is available 
    ' Replace www.yoursite.com with a site that
    ' is guaranteed to be online - perhaps your 
    ' corporate site, or microsoft.com
    Dim objUrl As New System.Uri("http://www.yoursite.com/")
    ' Setup WebRequest
    Dim objWebReq As System.Net.WebRequest
    objWebReq = System.Net.WebRequest.Create(objUrl)
    Dim objResp As System.Net.WebResponse
    Try
        ' Attempt to get response and return True
        objResp = objWebReq.GetResponse
        objResp.Close()
        objWebReq = Nothing
        Return True
    Catch ex As Exception
        ' Error, exit and return False
        objResp.Close()
        objWebReq = Nothing
        Return False
    End Try


'Here’s how you might use this function in your application:

If IsConnectionAvailable() = True Then
    MessageBox.Show("You are online!")
End If
0

The following will check network connection availability and Internet connection both :

If My.Computer.Network.IsAvailable Then

    Try
        If My.Computer.Network.Ping("www.Google.com") Then
            Infolabel.Text = "Computer is connected to the internet"
        Else
            Infolabel.Text = "Computer is not connected to the internet"
        End If
    Catch

    End Try

Else
     Infolabel.Text = "Computer is not connected to the internet"
End If
Bertrand Martel
  • 38,018
  • 15
  • 115
  • 140
Nishad
  • 1
  • 3