Microsoft.VisualBasic.Devices.Network.IsAvailable As Boolean
义马网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联公司2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
生成实例后可以通过此属性来获取是否联网的值,True代表已联网,False代表没有联网
Microsoft.VisualBasic.Devices.Network.NetworkAvailabilityChanged(ByVal Object, ByVal Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)
该事件是在电脑断开/连上互联网是引发的
其中:
e.IsNetworkAvailable
的功能同Microsoft.VisualBasic.Devices.Network.IsAvailable
实例:
Dim netListener As New Microsoft.VisualBasic.Devices.Network '生成实例
If netListener.IsAvailable = True Then
MsgBox("已联网")
Else
MsgBox("未联网")
End If
NetworkAvailabilityChanged实例:
WithEvents netListener As New Microsoft.VisualBasic.Devices.Network '生成实例
Private Sub OnNetworkChanged(ByVal Object, ByVal Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)Handles netListener.NetworkAvailabilityChanged
If e.IsNetworkAvailable = True Then
MsgBox("已联网")
Else
MsgBox("未联网")
End If
End Sub
新建工程,加入一模块,代码如下:
Option Explicit
Public isolerr As Boolean
Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
Public Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
'
Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 32
'
Public Type RASCONN95
dwSize As Long
hRasCon As Long
szEntryName(RAS95_MaxEntryName) As Byte
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
'
Public Type RASCONNSTATUS95
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
Public Function IsConnected() As Boolean
Dim TRasCon(255) As RASCONN95
Dim lg As Long
Dim lpcon As Long
Dim RetVal As Long
Dim Tstatus As RASCONNSTATUS95
'
TRasCon(0).dwSize = 412
lg = 256 * TRasCon(0).dwSize
'
RetVal = RasEnumConnections(TRasCon(0), lg, lpcon)
If RetVal 0 Then
MsgBox "出现错误!", vbCritical + vbDefaultButton1 + vbOKOnly, "错误!"
Form1.wangluozhuangkuang.Visible = False
isolerr = True
Exit Function
End If
'
isolerr = False
Tstatus.dwSize = 160
RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)
If Tstatus.RasConnState = H2000 Then
IsConnected = True
Else
IsConnected = False
End If
End Function
在窗体中添加一记时器(Timer),代码如下:
Private Sub Timer1_Timer()
checkonline
End Sub
然后在通用声明中添加如下代码
Sub checkonline()
If isolerr = False Then
If IsConnected = True Then
wangluozhuangkuang.Caption = "已经连接网络"
Else
wangluozhuangkuang.Caption = "尚未连接网络"
End If
End If
End Sub
最后在窗体上添加一Label,名字为wangluozhuangkuang
不懂再问我
既然从网上找的实例没问题,也许就是调用的时候没出来或者在检测网卡当中出现了问题。
'请试看这个 添加 Command1 修改下面你要检测的IP地址
Private Sub Command1_Click()
MsgBox IIf(CheckServ("192.168.1.1"), "服务器联线中", "服务器未联线") '你的服务器IP地址
End Sub
Public Function CheckServ(Ipadr As String) As Boolean
Dim aa$, bb$, fname$
CheckServ = False
fname = "c:\tmpfile.txt"
Open "c:\testip.bat" For Output As #1
Print #1, "@echo off"
Print #1, "ping " Ipadr " " fname
Close #1
If Dir(fname) "" Then Kill fname
Call Shell("cmd /c " "c:\testip.bat", vbHide)
'延时5秒内是否得到文档
starttm = Timer
Do
DoEvents
If Dir(fname) "" Then
If FileLen(fname) 10 Then Exit Do '得到文档即退出检测
End If
Loop Until Timer = starttm + 5
bb = ""
If Dir(fname) "" Then
Open fname For Input As #1
While Not EOF(1)
Line Input #1, aa
bb = bb aa vbCrLf
Wend
Close #1
End If
CheckServ = IIf(InStr(bb, "Request") 0, False, True)
If Dir("c:\testip.bat") "" Then Kill "c:\testip.bat"
End Function
你这段代码可以获得该主机名下的所有ip,每个ip地址都是有类型簇的,可以区别本地地址和网络地址、ip6地址。
你获取索引为0的ip,不一定是正确的网络地址,它有时顺序在中间,在后面。
应该获取所有ip,再通过ip地址簇类型区分
判断连接本身有一个函数,不过有时没联网也是真。
可以用ping,ping百度、ping访问速度快、不经常维护断线的网络主机。