如何检查特定IP地址是否连接到网络

LUK*_*KER 3 .net c# network-programming wifi

我想创建一个应用程序来查找网络上的特定 IP 地址是否在线。我已经知道IP了。我对 C# 很陌生,但想知道是否有人可以给我一个简单的解决方案。谢谢。

pij*_*olu 5

Ping ping = new Ping ();
IPAddress address = IPAddress.Parse("000.000.000.000");
PingReply pong = pingSender.Send(address);
Run Code Online (Sandbox Code Playgroud)

pong对象包含是否成功的信息。

if (pong.Status == IPStatus.Success)
{
  // your machine at address is up and responding
}
Run Code Online (Sandbox Code Playgroud)

一个完整的程序将使用这个

using System;
using System.Net;
using System.Net.NetworkInformation;

public class Program
{
    public static void Main()
    {
        Ping ping = new Ping();
        //change the following ip variable into the ip adress you are looking for
        string ip = " ";
        IPAddress address = IPAddress.Parse(ip);
        PingReply pong = ping.Send(address);
        if (pong.Status == IPStatus.Success)
        {
            Console.WriteLine(ip + " is up and running.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)