winInet.dll判断计算机是否能够连接到Internet的API
[System.Runtime.InteropServices.DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);
[System.Runtime.InteropServices.DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);
/// <summary>
/// 【方法一】判断本地的连接状态
/// </summary>
/// <returns></returns>
public bool LocalConnectionStatus()
{
System.Int32 i = new Int32();
if (!InternetGetConnectedState(ref i, 0))
{
Console.WriteLine("LocalConnectionStatus--未连网!");
return false;
}
else
{
if ((i & 1) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用调制解调器上网。");
return true;
}
else if ((i & 2) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用网卡上网。");
return true;
}
}
}
/// <summary>
/// 【方法二】检查网络连接状态
/// </summary>
/// <param name="connectionDescription">连接说明</param>
/// <param name="reservedValue">保留值</param>
/// <returns>True:连接成功 False:连接失败</returns>
[DllImport("wininet")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
public void NetworkConnectionStatus()
{
int i = 0;
if (InternetGetConnectedState(out i, 0))
{
Console.WriteLine("网络连接成功!");
}
else
{
Console.WriteLine("网络连接失败,请检查您的网络!");
}
}