【发布时间】:2008-09-12 18:40:05
【问题描述】:
.net 2.0 中是否有办法发现运行我的代码的机器的网络别名?具体来说,如果我的工作组将我的机器视为 //jekkedev01,我如何以编程方式检索该名称?
【问题讨论】:
标签: networking .net-2.0 alias
.net 2.0 中是否有办法发现运行我的代码的机器的网络别名?具体来说,如果我的工作组将我的机器视为 //jekkedev01,我如何以编程方式检索该名称?
【问题讨论】:
标签: networking .net-2.0 alias
由于您可以有多个网络接口,每个网络接口可以有多个 IP,并且任何单个 IP 可以有多个可以解析的名称,因此可能不止一个。
如果您想知道您的 DNS 服务器通过哪些名称知道您的机器,您可以像这样遍历它们:
public ArrayList GetAllDnsNames() {
ArrayList names = new ArrayList();
IPHostEntry host;
//check each Network Interface
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
//check each IP address claimed by this Network Interface
foreach (UnicastIPAddressInformation i in nic.GetIPProperties().UnicastAddresses) {
//get the DNS host entry for this IP address
host = System.Net.Dns.GetHostEntry(i.Address.ToString());
if (!names.Contains(host.HostName)) {
names.Add(host.HostName);
}
//check each alias, adding each to the list
foreach (string s in host.Aliases) {
if (!names.Contains(s)) {
names.Add(s);
}
}
}
}
//add "simple" host name - above loop returns fully qualified domain names (FQDNs)
//but this method returns just the machine name without domain information
names.Add(System.Net.Dns.GetHostName());
return names;
}
【讨论】:
如果您需要计算机描述,它存储在注册表中:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters
srvcomment
REG_SZ (string)
AFAIK 与任何域服务器或 PC 所连接的网络无关。
对于与网络相关的任何内容,我都使用以下内容:
System.Environment.MachineName
System.Net.Dns.GetHostName()
System.Net.Dns.GetHostEntry("LocalHost").HostName
如果 PC 有多个 NETBIOS 名称,我不知道有什么其他方法,只能根据它们解析到的 IP 地址对名称进行分组,如果 PC 有多个网络接口,即使这样也不可靠。
【讨论】:
我不是 .NET 程序员,但 System.Net.DNS.GetHostEntry 方法看起来像您需要的。它返回一个包含Aliases 属性的IPHostEntry 类的实例。
【讨论】:
使用System.Environment 类。它有一个用于检索机器名称的属性,该名称是从 NetBios 中检索的。除非我误解了你的问题。
【讨论】:
或 My.Computer.Name
【讨论】: