-1

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

In console application I need to pass the ip of my machine so what argument do I need to pass?

var myIp = Convert.ToString("");

if (search = value1.Contains(myIp))
{
    foo..
    foo..
}

So what argument should I send like I am using IPAddress but is not giving my ip?

Community
  • 1
  • 1
Sohail
  • 780
  • 3
  • 14
  • 27
  • 1
    Are you asking "how do I find the ip of my machine?" – weston Oct 13 '12 at 15:57
  • Sorry, what are you trying to do exactly? Can you show the code with the IPAddress? – cirrus Oct 13 '12 at 16:00
  • yes the ip of my machine just from system.networkinfomation we have parameters right?? – Sohail Oct 13 '12 at 16:00
  • Yes. Actually, system.networkinformation is even better. There's a discussion here: http://stackoverflow.com/questions/151231/how-do-i-get-the-local-network-ip-address-of-a-computer-programmatically-c – cirrus Oct 13 '12 at 17:41

2 Answers2

0

Here's how to get the IP (IPv4) addresses of your machine (you may have more than one):

var myAddresses = Dns.GetHostEntry(Dns.GetHostName())
                     .AddressList
                     .Where(e => e.AddressFamily == AddressFamily.InterNetwork);
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
0

I wouldn't use Dns.GetHostEntry() as this goes is a DNS network call, and it may fail under some circumstances. If it were me, I wouldn't query through the DNS system I'd just use WMI to read the data on the local hardware. Here's a post that does this;

http://kodefumaster.wordpress.com/2009/05/01/getting-the-network-adaptor-mac-ip-address-with-wmi/

Remember that each network card might have multiple IP addresses as well.

[Updated]

...or even easier, System.Net.NetworkInformation as you've already noted in your comments. See here;

How do I get the Local Network IP address of a computer programmatically? (C#)

But I wouldn't use DNS unless you specifically want to.

Community
  • 1
  • 1
cirrus
  • 5,624
  • 8
  • 44
  • 62