欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#检测本机是否联网

程序员文章站 2024-01-28 22:05:28
public class Net { [DllImport("wininet")] private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); /// ......

public class Net
{
[DllImport("wininet")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

/// <summary>
/// 检测本机是否联网
/// </summary>
/// <returns></returns>
public static bool IsConnectedInternet()
{
int i = 0;
if (InternetGetConnectedState(out i, 0))
{
//已联网
return true;
}
else
{
//未联网
return false;
}

}

}