C# 判断系统系统是否联网

C#判断系统是否联网

写这篇博客记录一下如何用C#判断系统是否联网,留作以后用时的参考。
利用Wininet.dll 中的InternetGetConnectedState函数。

// wininet API
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

 public static bool IsOnLine()
{
    try
    {
        var netstatus = string.Empty;
        var connection = 0;
        if (!InternetGetConnectedState(out connection, 0)) return false;
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}
发布了26 篇原创文章 · 获赞 41 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/anonymous_qsh/article/details/78619016