Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我可以从完全限定名称(可以带或不带域名)中获得一个简单的计算机名称(不带域名)吗?计算机名称中是否可以带有点 (.) 符号?
(这个问题似乎在做相反的事情)
没有主机名不能包含点(参考Wikipedia和RFC 952(参见“假设”)和RFC 1123)。它是主机名和域名之间的分隔符。所以你可以简单地做
string fullName = "foobar.domain"; string hostName = fullName.Substring(0, fullName.IndexOf('.'));
(当然,通过适当的错误检查,对于“fullName”实际上不是全名的情况)。
从 fqdn 中:
string s = "some.computer.name"; string host = s.Substring(0, s.IndexOf('.'));
框架外:
System.Net.Dns.GetHostName();