如何从c#中的完整计算机名称中获取一个简单的计算机名称(没有域名)?

Lou*_*hys 5 c# dns hostname fqdn

我是否可以从完全限定的名称(可以带或不带域名)获得一个简单的计算机名称(没有域名)?计算机名称是否可以在其中加上点(.)符号?

(这个问题好像反过来了)

Chr*_*n.K 6

没有主机名不能包含点(参考WikipediaRFC 952(参见"假设")和RFC 1123).它是主机名和域名之间的分隔符.所以你可以做到

string fullName = "foobar.domain";
string hostName = fullName.Substring(0, fullName.IndexOf('.'));
Run Code Online (Sandbox Code Playgroud)

(当然,正确的错误检查,对于"fullName"实际上不是全名的情况).


Sta*_*Who 5

出了一个fqdn:

string s = "some.computer.name";
string host = s.Substring(0, s.IndexOf('.'));
Run Code Online (Sandbox Code Playgroud)

走出框架:

System.Net.Dns.GetHostName();
Run Code Online (Sandbox Code Playgroud)