在.NET中获取不合格的主机名

发布于 2024-07-13 18:47:40 字数 736 浏览 5 评论 0原文

是否有相当于winsock函数的.NET GetNameInfoW() 是否设置了 NI_NOFQDN 标志? 正如 GetNameInfoW 的 MSDN 文档所解释的:

设置 NI_NOFQDN 标志会导致本地主机在 pNodeBuffer 参数中仅返回其相对可分辨名称 (RDN)。

我能找到的最接近的是System.Net.Dns.GetHostEntry(),它返回一个IPHostEntry,但其HostName主机的完全限定域名。 我只想要不合格的主机名。

例如,如果 IP 地址“xxxx”解析为 FQDN“foohost.company.domain.com”,我可以通过提供 NI_NOFQDNGetNameInfoW() 获取“foohost” ,但 .NET 中似乎没有等效的东西。 我不确定 GetNameInfoW() 是否在幕后执行 NetBIOS 或 LDAP 或其他操作。 关于如何在 .NET 中做同样的事情有什么想法吗?

Is there a .NET equivalent to the winsock function GetNameInfoW() with the NI_NOFQDN flag set? As the MSDN docs for GetNameInfoW explain:

Setting the NI_NOFQDN flag results in local hosts having only their Relative Distinguished Name (RDN) returned in the pNodeBuffer parameter.

The closest thing I can find is System.Net.Dns.GetHostEntry(), which returns an IPHostEntry but whose HostName is the fully-qualified domain name of the host. I just want the unqualified host name.

For example, if IP address "x.x.x.x" resolves to the FQDN "foohost.company.domain.com", I can get "foohost" from GetNameInfoW() by supplying NI_NOFQDN, but there doesn't seem to be an equivalent in .NET. I'm not sure if GetNameInfoW() is doing NetBIOS or LDAP or something else under the covers. Any thoughts on how to do the same in .NET?

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

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

发布评论

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

评论(2

热血少△年 2024-07-20 18:47:40

您是否正在尝试获取计算机的名称或计算机 IP 的 DNS 名称(有时不同)。 如果您确实想使用 GetNameInfoW(),您始终可以使用 .NET 中的 Pinvoke。

编辑:这可能是一个黑客行为,但是您可以使用 System.Net.Dns.GetHostEntry() 的结果并只执行 string.Split('.') 并将索引 0 作为不合格的姓名?

Are you trying to get the computer's name, or the DNS name for the computers IP (sometimes different). If you really wanted to use GetNameInfoW() you could always just use Pinvoke from .NET.

Edit: This might be a hack, but could you use the result from System.Net.Dns.GetHostEntry() and just do a string.Split('.') and take index 0 as the unqualified name?

浮华 2024-07-20 18:47:40

如果计算机有多个 IP 地址,它也可以有多个主机名(在这种情况下,完全限定与否并不重要)。

如果我需要“计算机名称”,我过去使用“Environment.MachineName”,从技术上讲,它返回盒子的 NetBIOS 名称,但至少这不依赖于 IP 配置。

If the computer has multiple IP-Adresses, it can have multiple hostnames as well (fully qualified or not doesn't matter in this context).

If I needed the "computer name" I used "Environment.MachineName" in the past, which technically returns the NetBIOS name of the box, but at least that is not dependend on the IP configuration.

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