در ویژوال بیسیک برای اینکه بررسی کنیم که آیا متصل به اینترنت هستیم یا خیر، می توان از کد زیر استفاده نمود:
کد اول (پیشنهاد شده):
1 2 3 4 5 |
If My.Computer.Network.IsAvailable Then MsgBox("Computer is connected.") Else MsgBox("Computer is not connected.") End If |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Public Function IsConnectionAvailable() As Boolean Dim objUrl As New System.Uri("http://www.google.com") Dim objWebReq As System.Net.WebRequest objWebReq = System.Net.WebRequest.Create(objUrl) Dim objresp As System.Net.WebResponse Try objresp = objWebReq.GetResponse objresp.Close() objresp = Nothing Return True Catch ex As Exception objresp = Nothing objWebReq = Nothing Return False End Try End Function |
1 2 3 4 5 6 7 8 9 10 11 |
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 |