如何从 C# 中的完整计算机名中获取简单的计算机名(不带域名)?

发布于 2025-01-08 14:19:52 字数 209 浏览 4 评论 0原文

我可以从完全限定名称(可以带或不带域名)中获取简单的计算机名称(不带域名)吗?计算机名称中是否可以包含点 (.) 符号?

(这个问题似乎做相反的事情)

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?

(this question seems to be doing the reverse)

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

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

发布评论

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

评论(3

我一向站在原地 2025-01-15 14:19:52

主机名不能包含点(参考 WikipediaRFC 952(参见“假设”)和 RFC 1123)。它是主机名和域名之间的分隔符。所以你可以简单地这样做

string fullName = "foobar.domain";
string hostName = fullName.Substring(0, fullName.IndexOf('.'));

(当然,对于“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

string fullName = "foobar.domain";
string hostName = fullName.Substring(0, fullName.IndexOf('.'));

(With proper error checking of course, for the case that "fullName" is not actually a fullname).

左岸枫 2025-01-15 14:19:52

超出 fqdn:

string s = "some.computer.name";
string host = s.Substring(0, s.IndexOf('.'));

超出框架:

System.Net.Dns.GetHostName();

Out of a fqdn:

string s = "some.computer.name";
string host = s.Substring(0, s.IndexOf('.'));

Out of the framework:

System.Net.Dns.GetHostName();
旧梦荧光笔 2025-01-15 14:19:52

通过 Split 函数将 GetHostName() 检索到的主机名拆分为结果数组的第 0 项:

string s = "some.computer.name";
string host = s.Split('.')[0];

Split the retrieved hostname from GetHostName() into by Split function, and the 0th item of the result array:

string s = "some.computer.name";
string host = s.Split('.')[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文