c#判断是否联网

方法一: 

using System; 

 using System.Collections.Generic; 

 using System.Linq; 

 using System.Text; 

 using System.Net.NetworkInformation; 

   

 namespace 判断是否联网 

 { 

     class Program 

     { 

         static void Main(string[] args) 

         { 

             Ping p = new Ping(); 

             PingReply pr; 

   

             pr = p.Send("119.75.218.45");//百度的IP 

             if (pr.Status != IPStatus.Success)//如果连接不成功 

             { 

                 Console.WriteLine("未联网"); 

             } 

             else

             { 

                 Console.WriteLine("已联网"); 

   

             } 

             Console.Read(); 

   

         } 

     } 

 }

 

 

c#关于判断网络连接正常与否的总结
 

本人最近做c#winform的项目,遇到了判断网络是否正常连接的问题。后来查出了以下几种方法,供大家学习参考。
1.方法二(用异步调用)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Demo : Form
    {
        public Demo()
        {
            InitializeComponent();
        }
        //判断
        private void btpanduan_Click(object sender, EventArgs e)
        {
            //210.192.120.228  163网易
            string ip = this.txtip.Text.ToString();
            int port = Convert .ToInt32( this.txtport.Text.ToString());
            bool a = panduan(ip, port );//135为本机服务端口号
            if (a == true)
            {
                MessageBox.Show("该网络连接正常 !");
            }
            else
            {
                MessageBox.Show("该网络连接不畅通 !");
            }
        }
     
      // 异步调用

        //判断的方法
        public bool panduan(string ip, int port)
        {
            try
            {
                TcpClient client = new TcpClient(ip, port);
                if (client.Connected)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}

2.利用 c# ping类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
    public partial class Demo3 : Form
    {
        public Demo3()
        {
            InitializeComponent();
        }
        System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
        System.Net.NetworkInformation.PingReply res;
        //检查网络连接
        private void btcheck_Click(object sender, EventArgs e)
        {

            string url = this.txturl.Text.ToString();
            bool a = check(url);
            if (a == true)
            {
                MessageBox.Show("连接成功!", "提示信息");

            }
            else
            {
                MessageBox.Show("连接失败!", "提示信息");
            }
        }

        public bool check(string url)
        {

            try
            {
                res = ping.Send(url);
                if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch {

                return false;
            }
        }
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值