如何使用c#.net
获取所有LAN系统的主机名通过“net view”命令获取一些系统主机名,即使它们在LAN中也无法访问所有系统。为什么呢?
答案 0 :(得分:1)
试试这个:(对于没有Active目录)
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;
}
答案 1 :(得分: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;
}