0

可能重复:
如何在 C# 中获取我自己的 IP 地址?

在 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。

4

2 回答 2

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);
                }
            }
        }
    }
}
于 2012-10-08T17:42:26.923 回答
0

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

 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);
  }
于 2012-10-08T17:41:34.777 回答