Pouvez-vous s’il vous plaît me dire s’il existe un moyen de vérifier s’il existe une connexion Internet sur mon ordinateur lorsque mon programme C # est en cours d’exécution. Pour un exemple simple, si Internet fonctionne, je générerais une boîte de message indiquant que l’ Internet is available
. sinon, je voudrais envoyer un message disant, Internet is unavailable
.
Sans utiliser la fonction de bibliothèque pour voir si le réseau est disponible (puisque cela ne vérifie pas la connectivité Internet)
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
Ou sans ouvrir une page Web et voir si elle renvoie les données
using (WebClient client = new WebClient()) htmlCode = client.DownloadSsortingng("http://google.com");
Parce que ces deux méthodes ci-dessus ne répondent pas à mes besoins.
une version un peu plus courte:
public static bool CheckForInternetConnection() { try { using (var client = new WebClient()) using (var stream = client.OpenRead("http://www.google.com")) { return true; } } catch { return false; } }
Une autre option est:
Ping myPing = new Ping(); Ssortingng 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 }
Vous pouvez trouver une discussion plus large ici
Considérez l’extrait de code suivant …
Ping myPing = new Ping(); Ssortingng 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 }
Bonne chance!
vient d’écrire des fonctions asynchrones pour le faire:
private void myPingCompletedCallback(object sender, PingCompletedEventArgs e) { if (e.Cancelled) return; if (e.Error != null) return; if (e.Reply.Status == IPStatus.Success) { //ok connected to internet, do something... } } private void checkInternet() { Ping myPing = new Ping(); myPing.PingCompleted += new PingCompletedEventHandler(myPingCompletedCallback); try { myPing.SendAsync("google.com", 3000 /*3 secs timeout*/, new byte[32], new PingOptions(64, true)); } catch { } }
Ma classe NetworkMonitor fournit maintenant ceci (basé sur d’autres réponses ici):
public bool IsInternetAvailable { get { return IsNetworkAvailable && _CanPingGoogle(); } } private static bool _CanPingGoogle() { const int timeout = 1000; const ssortingng host = "google.com"; var ping = new Ping(); var buffer = new byte[32]; var pingOptions = new PingOptions(); try { var reply = ping.Send(host, timeout, buffer, pingOptions); return (reply != null && reply.Status == IPStatus.Success); } catch (Exception) { return false; } }
Ceci est mon approche
Vérifiez si nous pouvons nous connecter à certains hôtes majeurs. Utilisez une solution de repli au cas où ce site ne serait pas disponible.
public static bool ConnectToInternet(int timeout_per_host_millis = 1000, ssortingng[] hosts_to_ping = null) { bool network_available = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); if (network_available) { ssortingng[] hosts = hosts_to_ping ?? new ssortingng[] { "www.google.com", "www.facebook.com" }; Ping p = new Ping(); foreach (ssortingng host in hosts) { try { PingReply r = p.Send(host, timeout_per_host_millis); if (r.Status == IPStatus.Success) return true; } catch { } } } return false; }
Remarques:
public static bool HasConnection() { try { System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("www.google.com"); return true; } catch { return false; } }