Tuesday, 29 October 2013

Get Machine name from IP Address c#

Description:

This article contains the C# code snippets to get Hostname or Machine name from IP Address and get IP Address from Machine name/Hostname.

Summary:

  1. Get Machine name from IP Address in C#
  2. Get Machine name from IPAddress in Command Prompt
  3. Get IPAddress from Machine name in C#

Get Machine name from IP Address in C#

You can use this C# function to convert IP Address into Hostname in C#
private static string GetMachineNameFromIPAddress(string ipAdress)
        {
            string machineName = string.Empty;
            try
            {
                IPHostEntry hostEntry=Dns.GetHostEntry(ipAdress);

                machineName=hostEntry.HostName;
            }
            catch (Exception ex)
            {
                // Machine not found...
            }
            return machineName;
        }

Get Hostname from IP Address using cmd ping Command

You can get Machine name or Hostname from IP Address using the following command
ping -a 192.168.56.101


Get IPAddress from Machine name(Hostname) in C#

You can use this C# function to convert Hostname into IP Address in C#
private static string GetIPAddressFromMachineName(string machineName)
        {
            string ipAdress = string.Empty;
            try
            {
                IPAddress[] ipAddresses = Dns.GetHostAddresses(machineName);

                IPAddress ip = ipAddresses[1];

                ipAdress = ip.ToString();
            }
            catch (Exception ex)
            {
                // Machine not found...
            }
            return ipAdress;
        }

Thanks,
Morgan
Software Developer

Advertisements
Advertisements

5 comments:

  1. &X48;i there, I fou&X6e;d yο&X75;г blog via Google whilе s&X65;arching fοr a si&X6d;ilaг
    subject, уouг wеbsitе came up,
    i&X74; appears goоԁ. I h&X61;vе bοo&X6B;mar&X6b;ed it
    in &X6D;y goo&X67;le b&X6F;oκmarks.
    Hi there, јust turneԁ іnto aware of
    youг b&X6c;og through Google, and
    founԁ th&X61;t it's rea&X6C;ly infor&X6D;atіve.
    Ι am going t&X6F; be caгеful
    for br&X75;ѕsels. Ι'll apprec&X69;ate ѕ&X68;оuld you ρroсeed th&X69;s in future.
    Many other &X66;οlks will like&X6C;у be benefited fro&X6d; your w&X72;iting.
    C&X68;eeгs!

    Revi&X65;ω &X6d;y &X77;ebpage ... the north face sverige

    ReplyDelete
  2. Hi everyone,
    May I know the c# program to get the list of ip address and host name of all computers connected to a LAN network.
    Thanks

    ReplyDelete
    Replies
    1. i want to get that c# program which capable to get list of ip address and host name of all computers connected to a wifi network.

      Delete
    2. Hi, check this article http://www.morgantechspace.com/2014/04/Get-List-of-Network-Computer-Names-in-C-Sharp.html to get ip address and host name of all computers connected to current network (LAN or Wifi)

      Delete