0

Possible Duplicate:
How to get my own IP address in C#?

In C# console application, I am using this code

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);
}

So in var reply = ping.Send(); what argument do I need to pass so as it should write the IP of the local machine? Here I need IP of the local machine in the argument.

Community
  • 1
  • 1
Sohail
  • 780
  • 3
  • 14
  • 27
  • 3
    Please [see the documentation first](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx). If there are any *remaining* questions not answered in the API documentation, then feel free to ask them. In addition, see this [How to: Ping a Host](http://msdn.microsoft.com/en-us/library/ms229713.aspx). –  Oct 08 '12 at 17:39
  • Here is how you can get your _local_ IP address : http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c – James Gaunt Oct 08 '12 at 17:40
  • Send doesn't "Write the ip of the local machine"--by "write the ip" I assume write tot he console... If that's not what you mean, please clarify. – Peter Ritchie Oct 08 '12 at 17:41
  • Just `Console.WriteLine("172.0.0.1");` and you have written your local IP address... – Servy Oct 08 '12 at 17:56

2 Answers2

1

This code does that:

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);
                }
            }
        }
    }
}
Joshua Smith
  • 134
  • 10
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

You can do in following way:

 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);
  }
Akash KC
  • 16,057
  • 6
  • 39
  • 59