How to Detect Working Internet Connection in C#

What is the best way to check for Internet connectivity using .NET?

public static bool CheckForInternetConnection(int timeoutMs = 10000, string url = null)
{
try
{
url ??= CultureInfo.InstalledUICulture switch
{
{ Name: var n } when n.StartsWith("fa") => // Iran
"http://www.aparat.com",
{ Name: var n } when n.StartsWith("zh") => // China
"http://www.baidu.com",
_ =>
"http://www.gstatic.com/generate_204",
};

var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.Timeout = timeoutMs;
using (var response = (HttpWebResponse)request.GetResponse())
return true;
}
catch
{
return false;
}
}

C# checking Internet connection

a little shorter version:

public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
catch
{
return false;
}
}

Another option is:

Ping myPing = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success) {
// presumably online
}

You can find a broader discussion here

How to detect working internet connection in C#?

Just use the plain function

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

that return true of false if the connection is up.

From MSDN:
A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.

Keep in mind connectivity is not all, you can be connected to a local network and the router is not connected to the Internet for instance. To really know if you are connected to the internet try the Ping class.

How to check the Internet connection with .NET, C#, and WPF

In the end I used my own code:

private bool CheckConnection(String URL)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Timeout = 5000;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
return true;
else
return false;
}
catch
{
return false;
}
}

An interesting thing is that when the server is down (I turn off my Apache) I'm not getting any HTTP status, but an exception is thrown. But this works good enough :)

How do I check for a network connection?

You can check for a network connection in .NET 2.0 using GetIsNetworkAvailable():

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

To monitor changes in IP address or changes in network availability use the events from the NetworkChange class:

System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged

check internet connection available or not in c#

You can check whether internet is available or not like this:

ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
if (internetConnectionProfile == null)
{
//logic ....
}

if (internetConnectionProfile != null)
{
this.IsInternetAvailable = internetConnectionProfile.GetNetworkConnectivityLevel() ==
NetworkConnectivityLevel.InternetAccess;

if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != 71)// Connection is not a Wi-Fi connection.
{
var isRoaming = internetConnectionProfile.GetConnectionCost().Roaming;

//user is Low on Data package only send low data.
var isLowOnData = internetConnectionProfile.GetConnectionCost().ApproachingDataLimit;

//User is over limit do not send data
var isOverDataLimit = internetConnectionProfile.GetConnectionCost().OverDataLimit;
IsWifiConnected = true;

}
else //Connection is a Wi-Fi connection. Data restrictions are not necessary.
{
IsWifiConnected = true;

}
}

Checking network status in C#

If you just want to check if the network is up then use:

bool networkUp
= System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

To check a specific interface's status (or other info) use:

NetworkInterface[] networkCards
= System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

To check the status of a remote computer then you'll have to connect to that computer (see other answers)

I need a event to detect Internet connect/disconnect

This is all covered (including the difference between being on the network and having the network connect you to the Internet) at http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx. I hope you meant to put that Windows 7 tag on your post, because all this is pretty new.

The key is INetworkListManager.get_IsConnectedToInternet() which pretty much does what it says on the tin. You have to jump around a bit to register for the events etc. The Code Pack wraps some of that up for you and has a network sample you can adapt.



Related Topics



Leave a reply



Submit