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

Discussion in '.NET' started by pinkerton1981, Oct 31, 2011.

  1. pinkerton1981

    pinkerton1981 New Member

    Joined:
    Oct 29, 2011
    Messages:
    20
    Likes Received:
    0
    Trophy 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]
     
  2. mahesh119

    mahesh119 New Member

    Joined:
    Sep 4, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for it can you upload a video tutorial for this .....?
     
  3. pinkerton1981

    pinkerton1981 New Member

    Joined:
    Oct 29, 2011
    Messages:
    20
    Likes Received:
    0
    Trophy 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!
     
  4. TheWicker

    TheWicker Make Money Online

    Joined:
    Nov 4, 2009
    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    6
    amm so we could put any url into that hostname "field"?
     

Share This Page