Check your internet connection

I need an application to check the Internet connection on my computer user. If there is, an image is displayed, and if not, another image is displayed. Here is the code I used to make this work:

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 

I decided to test this on my own computer by changing my default gateway (thereby, it looks like I lost the connection). But I realized that the code still showed that I was connected. Therefore, I think that this is only a test of the connectivity of the interface, which in this case is my connection to the router (this is true, I am connected to the router).

So the question is: how to check if the user PC is really connected to the Internet? I read an article. What is the best way to test your Internet connection using .NET? but this is in C # and I don't get it.

+4
source share
6 answers

You can use this tool to translate C # to VB.NET or vice versa:

 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 

By the way, the NetworkInterface.GetIsNetworkAvailable method that you used checks if any network connection is available or not - not Internet connection.

A network connection is considered accessible if any network interface is marked “up” and is not a loopback or tunnel interface.

+6
source

Or use this code

 If My.Computer.Network.IsAvailable Then MsgBox("Computer is connected.") Else MsgBox("Computer is not connected.") End If 
+2
source
 If My.Computer.Network.Ping("www.Google.com") Then ... End If 
+1
source

You can use this one , which should help you in VB and C # versions:

 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 
0
source
 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 
0
source

Below is information about network connectivity and Internet connectivity:

 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 
0
source

Source: https://habr.com/ru/post/1390185/


All Articles