-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkHelper.cs
38 lines (33 loc) · 1.06 KB
/
NetworkHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Net.NetworkInformation;
using System.Net;
using System;
public class NetworkHelper
{
public static bool IsNetworkAvailable(out string errorMessage)
{
errorMessage = null;
try
{
if (!NetworkInterface.GetIsNetworkAvailable())
{
errorMessage = "No network connection available.";
return false;
}
// Check internet connectivity by pinging a reliable server
const string reliableServer = "8.8.8.8"; // Google DNS server
var ping = new Ping();
var reply = ping.Send(reliableServer);
if (reply == null || reply.Status != IPStatus.Success)
{
errorMessage = "Could not ping a reliable server. Check your internet connection.";
return false;
}
return true;
}
catch (Exception ex)
{
errorMessage = "An error occurred while checking network connectivity: " + ex.Message;
return false;
}
}
}