在.net中查找网络别名

发布于 2024-07-05 05:57:13 字数 92 浏览 6 评论 0原文

.net 2.0 中有没有办法发现我的代码运行所在的计算机的网络别名? 具体来说,如果我的工作组将我的计算机视为 //jekkedev01,我如何以编程方式检索该名称?

Is there a way in .net 2.0 to discover the network alias for the machine that my code is running on? Specifically, if my workgroup sees my machine as //jekkedev01, how do I retrieve that name programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦与时光遇 2024-07-12 05:57:13

如果您需要计算机描述,它存储在注册表中:

  • 键:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters
  • 值名称:srvcomment
  • 数据类型:REG_SZ(字符串)

据我所知,它与任何域服务器或 PC 所连接的网络无关。

对于与网络相关的任何内容,我使用以下内容:

  • NETBIOS 名称:System.Environment.MachineName
  • 主机名:System.Net.Dns.GetHostName()
  • DNS 名称: System.Net.Dns.GetHostEntry("LocalHost").HostName

如果 PC 有多个 NETBIOS 名称,我不知道任何其他方法,只能根据它们解析到的 IP 地址对名称进行分组,如果 PC 具有多个网络接口,那么即使这样也是不可靠的。

If you need the computer description, it is stored in registry:

  • key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters
  • value name: srvcomment
  • data type: REG_SZ (string)

AFAIK it has nothing to do with any domain server, or with the network the PC is attached to.

For anything related to the network, I am using the following:

  • NETBIOS name: System.Environment.MachineName
  • host name: System.Net.Dns.GetHostName()
  • DNS name: System.Net.Dns.GetHostEntry("LocalHost").HostName

If the PC has multiple NETBIOS names, I do not know any other method but to group the names based on the IP address they resolve to, and even this is not reliable if the PC has multiple network interfaces.

黎歌 2024-07-12 05:57:13

我不是 .NET 程序员,而是 系统.Net.DNS.GetHostEntry 方法看起来像您所需要的。 它返回 IPHostEntry 类的实例,其中包含别名 属性。

I'm not a .NET programmer, but the System.Net.DNS.GetHostEntry method looks like what you need. It returns an instance of the IPHostEntry class which contains the Aliases property.

心安伴我暖 2024-07-12 05:57:13

使用 System.Environment 类。 它有一个用于检索机器名称的属性,该名称是从 NetBios 检索的。 除非我误解了你的问题。

Use the System.Environment class. It has a property for retrieving the machine name, which is retrieved from the NetBios. Unless I am misunderstanding your question.

橘香 2024-07-12 05:57:13

或我的计算机名称

or My.Computer.Name

忘你却要生生世世 2024-07-12 05:57:13

由于您可以有多个网络接口,每个网络接口可以有多个 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;
}

Since you can have multiple network interfaces, each of which can have multiple IPs, and any single IP can have multiple names that can resolve to it, there may be more than one.

If you want to know all the names by which your DNS server knows your machine, you can loop through them all like this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文