What is the best way to determine if there is a network connection available?
Asked
Active
Viewed 1.2e+01k times
117
-
Possible duplicate of [What is the best way to check for Internet connectivity using .NET?](https://stackoverflow.com/questions/2031824/what-is-the-best-way-to-check-for-internet-connectivity-using-net) – T.Todua Sep 09 '19 at 11:12
4 Answers
188
The marked answer is 100% fine, however, there are certain cases when the standard method is fooled by virtual cards (virtual box, ...). It's also often desirable to discard some network interfaces based on their speed (serial ports, modems, ...).
Here is a piece of code that checks for these cases:
/// <summary>
/// Indicates whether any network connection is available
/// Filter connections below a specified speed, as well as virtual network cards.
/// </summary>
/// <returns>
/// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
/// </returns>
public static bool IsNetworkAvailable()
{
return IsNetworkAvailable(0);
}
/// <summary>
/// Indicates whether any network connection is available.
/// Filter connections below a specified speed, as well as virtual network cards.
/// </summary>
/// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
/// <returns>
/// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
/// </returns>
public static bool IsNetworkAvailable(long minimumSpeed)
{
if (!NetworkInterface.GetIsNetworkAvailable())
return false;
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
// discard because of standard reasons
if ((ni.OperationalStatus != OperationalStatus.Up) ||
(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
continue;
// this allow to filter modems, serial, etc.
// I use 10000000 as a minimum speed for most cases
if (ni.Speed < minimumSpeed)
continue;
// discard virtual cards (virtual box, virtual pc, etc.)
if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
continue;
// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
continue;
return true;
}
return false;
}

Community
- 1
- 1

Simon Mourier
- 132,049
- 21
- 248
- 298
-
17this is correct and deserves many upvotes - I found that the NetworkAvailabilityChanged event mentioned in the accepted answer is not reliably fired, which is probably caused by virtual adapters as mentioned in this answer – Marek Feb 09 '12 at 13:15
-
1+1 for the additional check for virtual machines. Just run into this case yesterday. But I still think this doesn't handle 'Hype-V', due to they don't have 'virtual' in their name. – Stefan Over Apr 23 '14 at 06:35
-
3@Herdo - yes, the code's not perfect, and I don't think there *can* be anything perfect in a virtual world. But you can improve it. – Simon Mourier Apr 23 '14 at 07:52
-
@SimonMourier You are right. Just wanted to note that, if anybody gets still returned true, if there is no connection --> This might be an issue :) – Stefan Over Apr 23 '14 at 07:56
-
There is a known bug btw if you are planning to use this code on Android. https://bugzilla.xamarin.com/show_bug.cgi?id=1969 – erkangur May 13 '14 at 16:39
-
The marked answer also gives an event you can subscribe to. Do you know of a reliable way of using an event to fire on network up/down? – noelicus Feb 13 '15 at 17:39
-
1@noelicus - On projects I have been working on so far, I have implemented a periodic monitoring/polling system (simple HTTP GET requests because I was targeting a web server) which raises events when the server is detected as "down" (not reachable by HTTP) or "up" (reachable again). In the general case, you can define what "reliable" means in your case, and implement a similar logic. – Simon Mourier Feb 13 '15 at 18:26
-
Missing: if (ni.Description.Equals("VMware Network", StringComparison.OrdinalIgnoreCase)) continue; – user1546570 Jun 09 '16 at 08:30
-
Fails for me with Hyper-V installed but running on host OS as GetAllNetworkInterfaces doesn't return the actual Ethernet adapter. Instead I get Hyper-V adapters. One for the guest OS and a 'vEthernet (Virtual Switch)' adapter. The latter is used by the main Ethernet adapter and so it seems the call won't return the main adapter but will return the virtual switch, which I think should be allowed through. – tjmoore Aug 16 '16 at 12:28
-
Fails with 'Microsoft Virtual Machine Bus Network Adapter' which is present on Win Server 2008 with what I suspect is VM host or guest using Hyper-V or equivalent. Customer detected issue and only adapters are this and two tunnel adapters. – tjmoore Jul 05 '17 at 16:26
-
Great answer! For performance reasons I suggest dropping the initial "NetworkInterface.GetIsNetworkAvailable()", as this is equally slow as "NetworkInterface.GetAllNetworkInterfaces()" and the implementation checks the same properties. – Frode Evensen Sep 17 '19 at 09:01
159
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

Mitch Wheat
- 295,962
- 43
- 465
- 541
-
9
-
This doesn't always work in WPF. Some laptops return false and some return true – krilovich Feb 10 '16 at 17:21
-
Is this really work? I just wrote this line and turn my router off and still returns true! – Mushfiq May 20 '17 at 23:37
-
1`NetworkInterface.GetIsNetworkAvailable()` has been very unreliable in my application (.NET 4.5, Windows 10), especially when running in a virtual machine. Handling the events from `NetworkAvailabilityChanged` has been reliable. – eskimwier Sep 14 '17 at 15:16
-
**That is not reliable.** read https://stackoverflow.com/a/25779403/2377343 – T.Todua Sep 09 '19 at 11:17
-
@T.Todua: have you seen this comment in the question you linked to? ; "This code only checks if the network cable is plugged in." Finding network connectivity under windows is difficult at the best of times. – Mitch Wheat Sep 09 '19 at 23:47
10
Microsoft windows vista and 7 use NCSI (Network Connectivity Status Indicator) technic:
- NCSI performs a DNS lookup on www.msftncsi.com, then requests http://www.msftncsi.com/ncsi.txt. This file is a plain-text file and contains only the text 'Microsoft NCSI'.
- NCSI sends a DNS lookup request for dns.msftncsi.com. This DNS address should resolve to 131.107.255.255. If the address does not match, then it is assumed that the internet connection is not functioning correctly.

Saleh Rahimzadeh
- 289
- 3
- 7
-
3This is great, but is not technically about the availability of a 'network' but more 'do I have internet connectivity via http?' You can have perfect network connections but no connectivity to the internet, for instance on a stand-alone lan. See for more info om NCSI: http://technet.microsoft.com/en-us/library/cc766017.aspx – MichielB Jan 16 '15 at 13:16
0
Call this method to check the network Connection.
public static bool IsConnectedToInternet()
{
bool returnValue = false;
try
{
int Desc;
returnValue = Utility.InternetGetConnectedState(out Desc, 0);
}
catch
{
returnValue = false;
}
return returnValue;
}
Put this below line of code.
[DllImport("wininet.dll")]
public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

Kumar
- 442
- 1
- 9
- 19