判断是否网络连通 .net Ping

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Ping p = new Ping();
            string hostname = "192.168.0.1";//目标IP
            int timeout = 3000;//超时时间
            Stopwatch ws = new Stopwatch();
            ws.Start();
            var result= p.Send(hostname, timeout);
            ws.Stop();
            if (result.Status == IPStatus.Success)
            {
                Console.WriteLine("网络连通");
            }
            else
            {
                Console.WriteLine("网络不通");
            }
            Console.WriteLine($"{ ws.ElapsedMilliseconds}");
            Console.WriteLine($"{ result.Status}");
            Console.ReadLine();
        }
    }
}

  

posted @ 2018-08-27 18:06  風飄絮℃  阅读(302)  评论(0编辑  收藏  举报