how to get host name of all LAN systems using c#.net
through "net view" command am getting some systems host names, not able to access all the systems even though they are in the LAN. why so?
Try this: (For no Active directory)
public static int GetAllIPAndHostNames()
{
string strHostName;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);
IPHostEntry remoteIP;
//using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
int i = 0;
while (i < addr.Length)
{
Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
//HostNames
remoteIP = Dns.GetHostEntry((addr[i]));
Console.WriteLine("HostName {0}: {1} ", i, remoteIP.HostName);
i++;
}
return 0;
}
private static List<String> getAllHostNames()
{
List<String> hostNames = new List<String>();
IPAddress[] ipaddress = Dns.GetHostAddresses(Dns.GetHostName());
String hname;
foreach (IPAddress ip in ipaddress)
{
//if ipv4
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
hname = Dns.GetHostEntry(ip).HostName.ToLower();
if (!hostNames.Contains(hname))
{
hostNames.Add(hname);
}
}
}
return hostNames;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.