如何从 C# 中的完整计算机名中获取简单的计算机名(不带域名)?
我可以从完全限定名称(可以带或不带域名)中获取简单的计算机名称(不带域名)吗?计算机名称中是否可以包含点 (.) 符号?
Can I get just a simple computer name (without the domain name) from a fully qualified name (can be with or without a domain name)? Is it possible for a computer name to have a dot (.) sign in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
主机名不能包含点(参考 Wikipedia 和 RFC 952(参见“假设”)和 RFC 1123)。它是主机名和域名之间的分隔符。所以你可以简单地这样做
(当然,对于“fullName”实际上不是全名的情况,需要进行适当的错误检查)。
No hostnames cannot contain a dot (reference Wikipedia and RFC 952 (see "ASSUMPTIONS") and RFC 1123). It is the delimiter between the hostname and the domainname. So you can simply do
(With proper error checking of course, for the case that "fullName" is not actually a fullname).
超出 fqdn:
超出框架:
Out of a fqdn:
Out of the framework:
通过 Split 函数将 GetHostName() 检索到的主机名拆分为结果数组的第 0 项:
Split the retrieved hostname from GetHostName() into by Split function, and the 0th item of the result array: