How to check if internet is avaiable in C# and VB.NET

To check if internet is avaiable in C# and VB.NET you can use the following snippet.

Sample C#

private static bool IsInternetAvaiable(string url)
{
	try
	{
		using (var client = new WebClient())
		using (var stream = client.OpenRead(url))
		{
			return true;
		}
	}
	catch (Exception ex)
	{
		//catch and handle the exception your way
		return false;
	}
}

Sample VB.NET

Private Shared Function IsInternetAvaiable(ByVal url As String) As Boolean
	Try 
		Dim client As var = New WebClient
		Dim stream As var = client.OpenRead(url)
		Return true
	Catch ex As Exception
		'catch and handle the exception your way
		Return false
	End Try
End Function

Please Share us:
Please rate this snippet

One thought on “How to check if internet is avaiable in C# and VB.NET

Leave a Reply