Jawaban:
Anda dapat memeriksa koneksi jaringan di .NET 2.0 menggunakan GetIsNetworkAvailable()
:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
Untuk memantau perubahan alamat IP atau perubahan ketersediaan jaringan menggunakan peristiwa dari kelas NetworkChange :
System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged
NetworkInterface.GetIsNetworkAvailable()
sangat tidak dapat diandalkan di aplikasi saya (.NET 4.5, Windows 10), terutama saat dijalankan di mesin virtual. Penanganan acara dari NetworkAvailabilityChanged
telah dapat diandalkan.
Jawaban yang ditandai adalah 100% baik-baik saja, namun, ada kasus tertentu ketika metode standar tertipu oleh kartu virtual (kotak virtual, ...). Seringkali juga diinginkan untuk membuang beberapa antarmuka jaringan berdasarkan kecepatannya (port serial, modem, ...).
Berikut adalah potongan kode yang memeriksa kasus-kasus ini:
/// <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;
}
Microsoft windows vista dan 7 menggunakan teknik NCSI (Network Connectivity Status Indicator):
Panggil metode ini untuk memeriksa koneksi jaringan.
public static bool IsConnectedToInternet()
{
bool returnValue = false;
try
{
int Desc;
returnValue = Utility.InternetGetConnectedState(out Desc, 0);
}
catch
{
returnValue = false;
}
return returnValue;
}
Letakkan ini di bawah baris kode.
[DllImport("wininet.dll")]
public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);