我正在编写一个应用程序,该应用程序将用于定期上/下网络连接的移动环境中。如何查看特定网络是否可用?
答案 0 :(得分:5)
尝试NetworkInterface.GetIsNetworkAvailable
:
指示是否有任何网络连接可用。
如果任何网络接口标记为“up”且不是环回或隧道接口,则认为网络连接可用。
答案 1 :(得分:3)
要查看是否有任何网络可用,您可以使用VB.NET My命名空间:
My.Computer.Network.IsAvailable
我猜这是Andrew的回答中NetworkInterface
属性的抽象。要查看是否可以使用可用网络访问特定服务器,可以使用
My.Computer.Network.Ping(host name or IP address, or a System.Uri)
答案 2 :(得分:0)
我喜欢挖掘旧线程!我的解决方案是使用DNS进行测试。这样,您可以测试xxx网络内的特定名称,以判断您是在内部/外部。嵌套的try语句显示了这个概念。
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