C#利用判断是否能够连接到Internet

2025-03-03 07:33:47
推荐回答(1个)
回答1:

private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = OnLine() ? "已连接" : "未连接";
        }
        public bool OnLine()
        {
            System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
            ///注:
            ///     选一个好的host ,这里只是举个例子
            System.Net.NetworkInformation.PingReply pr = ping.Send("wjshan0808.3vhost.net", 12000); 
            return pr.Status == System.Net.NetworkInformation.IPStatus.Success;
        }