开发者

Check Network Availablility VB.net

I'm writing an application that will be used in a mobile environment whe开发者_如何学运维re network connectivity will be up/down regularly. How do I check to see that a specific network is available?


Try NetworkInterface.GetIsNetworkAvailable:

Indicates whether any network connection is available.

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


To see if any network is available you can use the VB.NET My namespace:

My.Computer.Network.IsAvailable

which I would guess is an abstraction of the NetworkInterface property in Andrew's answer. To see if you can use an available network to get to a specific server, you can use

My.Computer.Network.Ping(host name or IP address, or a System.Uri)


I love digging up old threads! My solution was to test with DNS. This way you can test for specific names inside xxx network to tell if you are inside/outside. The nested try statements shows this concept.

Imports System.Net

Module Networker

Dim Online_Status As Boolean = vbFalse
Dim InsideJoeNetwork As Boolean = vbFalse
Dim CurrentJoeIPAddress As New IPHostEntry



Public ReadOnly Property GetOnlineStatus() As String
    Get
        Return Online_Status
    End Get

End Property



Public ReadOnly Property InsideJoeNet() As String
    Get
        Return InsideJoeNetwork
    End Get

End Property



Sub Initialize()
    Set_Online_Status()

End Sub



Public Sub Set_Online_Status()

    If My.Computer.Network.IsAvailable Then
        Try
            Dim DNSTest As IPHostEntry = Dns.GetHostEntry("google.com")
            If DNSTest.AddressList.Length > 0 Then
                Online_Status = True
                Detect_Joe_Network()
            Else : Online_Status = False

            End If

        Catch ex As System.Net.Sockets.SocketException

            Online_Status = False

        End Try
    End If

End Sub



Public Sub Detect_Joe_Network()

    If Online_Status = True Then

        Dim JoeIP As IPHostEntry = New IPHostEntry()

        Try
            JoeIP = Dns.GetHostEntry("laptop")
            If JoeIP.AddressList.Length > 0 Then

                InsideJoeNetwork = True
                CurrentJoeIPAddress = JoeIP
                'MessageBox.Show(JoeIP.HostName.ToString(), "JoeIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Sockets.SocketException

            Try
                JoeIP = Dns.GetHostEntry("laptop.exampledomain.com")
                If JoeIP.AddressList.Length > 0 Then

                    InsideJoeNetwork = False
                    CurrentJoeIPAddress = JoeIP
                    ' MessageBox.Show(JoeIP.HostName.ToString(), "JoeIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Catch ey As Sockets.SocketException

            End Try
        End Try
    End If

End Sub

End Module
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜