How to check the Internet connection with .NET, C#, and WPF

后端 未结 7 1486
陌清茗
陌清茗 2020-11-30 04:51

I am using .NET, C# and WPF, and I need to check whether the connection is opened to a certain URL, and I can\'t get any code to work that I have found on the Internet.

相关标签:
7条回答
  • 2020-11-30 05:43

    Use this:

    private bool CheckConnection()
    {
        WebClient client = new WebClient();
        try
        {
            using (client.OpenRead("http://www.google.com"))
            {
            }
            return true;
        }
        catch (WebException)
        {
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题