Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether you are connected to Internet or not in C#?
There are several ways to check whether a machine is connected to the internet in C#. The most common approach uses the System.Net namespace, which provides methods for sending and receiving data from resources identified by a URI. The WebClient or HttpClient classes are particularly useful for this purpose.
The technique involves making a request to a reliable URL and checking if the response is successful. Google's http://google.com/generate_204 endpoint is commonly used because it returns a minimal response, making it efficient for connectivity checks.
Using WebClient for Internet Connectivity Check
Example
using System;
using System.Net;
class Program {
static void Main(string[] args) {
if (IsConnectedToInternet()) {
Console.WriteLine("Connected to the internet");
} else {
Console.WriteLine("No internet connection");
}
}
public static bool IsConnectedToInternet() {
try {
using (var client = new WebClient())
using (client.OpenRead("http://google.com/generate_204")) {
return true;
}
}
catch {
return false;
}
}
}
The output of the above code is −
Connected to the internet
Using HttpClient for Modern Applications
For modern .NET applications, HttpClient is preferred over WebClient as it provides better performance and async support −
Example
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program {
static async Task Main(string[] args) {
bool isConnected = await IsConnectedToInternetAsync();
Console.WriteLine(isConnected ? "Internet connection available" : "No internet connection");
}
public static async Task<bool> IsConnectedToInternetAsync() {
try {
using (var client = new HttpClient()) {
client.Timeout = TimeSpan.FromSeconds(5);
var response = await client.GetAsync("http://google.com/generate_204");
return response.IsSuccessStatusCode;
}
}
catch {
return false;
}
}
}
The output of the above code is −
Internet connection available
Using NetworkAvailabilityChanged Event
For applications that need to monitor connectivity changes continuously, you can use the NetworkAvailabilityChanged event −
Example
using System;
using System.Net;
using System.Net.NetworkInformation;
class Program {
static void Main(string[] args) {
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
Console.WriteLine("Initial connectivity: " + (NetworkInterface.GetIsNetworkAvailable() ? "Available" : "Not Available"));
Console.WriteLine("Monitoring network changes...");
// Keep the application running for demonstration
System.Threading.Thread.Sleep(10000);
}
private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e) {
Console.WriteLine("Network availability changed: " + (e.IsAvailable ? "Available" : "Not Available"));
}
}
The output of the above code is −
Initial connectivity: Available Monitoring network changes...
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| WebClient | Simple to use, synchronous | Deprecated, blocking operation |
| HttpClient | Modern, async support, better performance | Requires async/await pattern |
| NetworkInterface | Instant check, no network request | Only checks local network, not internet connectivity |
Conclusion
Checking internet connectivity in C# can be accomplished through various methods, with HttpClient being the preferred modern approach for its async capabilities and better performance. For real-time monitoring, combine NetworkInterface checks with actual HTTP requests to reliable endpoints like Google's generate_204 service.
