C# Check If Internet Connection is Available
Networking - C# Checking Internet Connection
While developing a web application in asp.net, this is very important to check whether the internet connection is available or not.
So in this post I will show how to detect working internet connection and what is the fastest and most efficient way to check for Internet connection in asp.net web application using C#. Find the below code :-
Read more about How to get IP Address of Visitors Machine in ASP.Net
Check Internet Connectivity |
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bool flag = CheckForInternetConnection();
if (flag == true)
{ Response.Write("Internet connection is working."); }
else
{ Response.Write("Internet connection is not working."); }
}
}
public bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}
}
I hope you will enjoy the development tips while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Also If you like this article, don't forget to share this article with your friends and colleagues.
Also If you like this article, don't forget to share this article with your friends and colleagues.
C# Check If Internet Connection is Available
Reviewed by Ravi Kumar
on
12:43 AM
Rating:
No comments: