C# 判断ip地址能否被ping通

            string ip = "127.0.0.1"; //要检查的IP地址
            Ping pingSender = new Ping();
            try
            {
                PingReply reply = pingSender.Send(ip, 120);
                if (reply.Status == IPStatus.Success)
                {
                    Console.WriteLine($"Ping {ip} 成功。");
                }
                else
                {
                    Console.WriteLine($"Ping {ip} 失败:{reply.Status}.");
                }
            }
            catch (PingException ex)
            {
                Console.WriteLine(ex.ToString());
            }

 

posted @ 2024-03-27 09:28  芈璐  阅读(162)  评论(0编辑  收藏  举报