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
- Get Machine name from IP Address in C#
- Get Machine name from IPAddress in Command Prompt
- 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
Morgan
Software Developer
Advertisement