【问题标题】:How to send the ping argument in C# [duplicate]如何在 C# 中发送 ping 参数 [重复]
【发布时间】:2012-09-28 23:57:50
【问题描述】:

可能重复:
How to get my own IP address in C#?

在 C# 控制台应用程序中,我正在使用此代码

var ping = new Ping();
var reply = ping.Send();
if (reply.Status == IPStatus.Success)
{
    Console.WriteLine("Pinging with server");
    Console.WriteLine("Press any key to continue");
    Console.ReadKey(true);
}

那么在var reply = ping.Send(); 中我需要传递什么参数才能写入本地机器的IP?这里我需要参数中本地机器的IP。

【问题讨论】:

  • see the documentation first。如果 API 文档中有任何 剩余 问题未得到解答,请随时提出。另外,看这个How to: Ping a Host
  • 您可以通过以下方式获取您的本地 IP 地址:stackoverflow.com/questions/1069103/…
  • 发送不“写本地机器的 ip”——通过“写 ip”我假设写到控制台......如果这不是你的意思,请澄清。跨度>
  • 只要Console.WriteLine("172.0.0.1");,你就写了你的本地IP地址...

标签: c# console-application


【解决方案1】:

这段代码就是这样做的:

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var ping = new Ping())
            { 
                var reply = ping.Send(IPAddress.Loopback);

                if (reply.Status == IPStatus.Success)
                {
                    Console.WriteLine("Pinging with server: " + reply.Address);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey(true);
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    您可以通过以下方式进行:

     IPAddress add = IPAddress.Loopback; /// Reuturns local Ip Address
     PingReply reply = ping.Send(add);
     if (reply.Status == IPStatus.Success)
     {
        Console.WriteLine("Pinging with server");
        Console.WriteLine("Press any key to continue");
        Console.ReadKey(true);
      }
    

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 2022-12-29
      • 2014-01-17
      • 2019-01-27
      • 1970-01-01
      • 2018-01-28
      • 2021-08-22
      • 1970-01-01
      • 2011-07-20
      相关资源
      最近更新 更多