[System.Runtime.InteropServices.DllImport("wininet")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
if (InternetGetConnectedState(out i, 0))
MessageBox.Show("联网状态");
else
MessageBox.Show("你网线掉了啦");
}
修改下
[System.Runtime.InteropServices.DllImport("wininet")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
//判断网络是连接到互联网
public static bool IsNetWorkConnect()
{
int i = 0;
return InternetGetConnectedState(out i, 0) ? true:false;
}
if ( IsNetWorkConnect() )
MessageBox.Show("已连接到互联网");
else
MessageBox.Show("无网络")