C# - How to get IP addresses of a computer?

pinkerton1981

New Member
Joined
Oct 29, 2011
Messages
20
Reaction score
0
Points
0
Hi All,

I'm new on MMD and I would like to share with you tips on C# and ASP.NET, since I have been working on those languages for a while and as I like them pretty much. So, here is my first tip!

To get IP addresses of a computer, use the method Resolve from the System.Net.Dns class. Here is a piece of code that uses it. To get IP addresses of the local computer, only is needed to call the method by supplying the computer name:

Code:
[COLOR=darkblue]

...
using System.Net;
...

string[] GetIPaddresses(string computerName)
{
   string[] saddr = null;
   IPAddress[] addr = [B]Dns.Resolve[/B](computerName).AddressList;

   if (addr.Length > 0)
   {
        saddr = new String[addr.Length];
        for (int i = 0; i < addr.Length; i++)
            saddr[i] = addr[i].ToString();
   }
   return saddr;
}

[/COLOR]


Hope that you like!

pinkerton1981

PS:
Code:
[COLOR=DarkBlue]String computerName = Dns.GetHostName();[/COLOR]
 

mahesh119

New Member
Joined
Sep 4, 2011
Messages
43
Reaction score
0
Points
0
Thanks for it can you upload a video tutorial for this .....?
 

pinkerton1981

New Member
Joined
Oct 29, 2011
Messages
20
Reaction score
0
Points
0
Hi mahesh119,

I think you don't need a video tutorial since the code I put is clear and simple!

Have a good day!
 

TheWicker

Make Money Online
Joined
Nov 4, 2009
Messages
53
Reaction score
0
Points
6
amm so we could put any url into that hostname "field"?