C#判断网络连接状态

DllImport("wininet.dll",EntryPoint="InternetGetConnectedState")]
public extern static bool InternetGetConnectedState(out int connectionDescription,int reservedValue);
//参数说明connectionDescription连接说明,reservedValue保留值

public bool IsConnectedToInternet()
{
    int Desc=0;
    return InternetGetConnectedState(out Desc,0);
}
判断网络状态的实现代码如下:

private void button2_Click(object sender,EventArgs e)
{
    if(IsConnectedToInternet())
    {
        MessageBox.Show("已连接在网上!","提示");
    }
     else
        MessageBox.Show("未连接在网上!!","提示");
}

你可能感兴趣的:(C#)