Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As mention in the subject,
im trying to do a code which allow me to retrieve a IP address of a computer in LAN by using its hostname.
Posted

C#
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry("COMPUTERNAME");
foreach (var ipAddress in host.AddressList)
{
    // Loop all ip-addresses. This object has a lot of properties v4/v6 etc..
    Console.WriteLine(ipAddress);
}
 
Share this answer
 
v3
Hi,

see this link for some code:
http://www.dijksterhuis.org/converting-an-ip-address-to-a-hostname-and-back-with-c/[^]
I can use this code to look up the IP-Address of a server called "Panic":
C#
private void button1_Click(object sender, EventArgs e)
{
   IPHostEntry NameToIpAddress;
   NameToIpAddress = Dns.GetHostEntry("Panic");
   int AddressCount = 0;
   foreach (IPAddress Address in NameToIpAddress.AddressList)
      Console.WriteLine("IP Address {0}: {1}", ++AddressCount, Address.ToString());
}
 
Share this answer
 
C#
string strHostName="localhost";
IPHostEntry ipEntry = Dns.GetHostByName( strHostName );
IPAddress [] addr = ipEntry.AddressList;

for( int i = 0; i < addr.Length; i++ )
{
Console.WriteLine( "IP Address {0}: {1} ", i, addr [ i ].ToString() );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900