检查Internet连接

时间:2012-01-10 07:55:51

标签: vb.net

我需要我的应用程序来检查用户计算机上的互联网连接。如果有,则显示图像,如果没有,则显示不同的图像。这是我用来实现这个目的的代码:

    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

我决定通过更改我的默认网关在我自己的计算机上测试它(从而使它看起来好像我失去了连接)。但我意识到代码仍显示我已连接。所以我认为它只是检查接口的连接 - 在这种情况下,是我与路由器的连接(这是真的,我连接到路由器)。

所以,问题是:如何检查用户的PC是否实际连接到互联网?我阅读了文章What is the best way to check for Internet connectivity using .NET?,但它是在C#中,我不明白。

6 个答案:

答案 0 :(得分:6)

您可以使用this tool将C#转换为VB.NET,反之亦然:

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

顺便说一句,您使用的NetworkInterface.GetIsNetworkAvailable方法会检查是否有任何网络连接可用 - Internet连接。

  

如果有任何网络,则认为网络连接可用   interface标记为“up”,不是环回或隧道接口。

答案 1 :(得分:3)

或使用此代码

If My.Computer.Network.IsAvailable Then
    MsgBox("Computer is connected.")
Else
    MsgBox("Computer is not connected.")
End If

答案 2 :(得分:1)

If My.Computer.Network.Ping("www.Google.com") Then
...
End If

答案 3 :(得分:0)

您可以使用this,这可以帮助您了解VB& C#版本:

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

答案 4 :(得分: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

答案 5 :(得分:0)

以下内容将检查网络连接可用性和Internet连接:

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