How to get a simple computer name (without a domain name) from the fully qualified computer name in C #?

Is it possible to get a simple computer name (without a domain name) from a full name (maybe with or without a domain name)? Is it possible for a computer name to have a dot (.) In it?

(this question seems to do the opposite)

+4
source share
2 answers

No host names can contain a period ( Wikipedia link and RFC 952 (see "ASSUMPTIONS") and RFC 1123 ). This is a separator between the host name and the domain name. Therefore you can just do

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

(With proper error checking, of course, for the case when "fullName" is actually not a full name).

+5
source

From fqdn:

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

Out of frame:

 System.Net.Dns.GetHostName(); 
+5
source

Source: https://habr.com/ru/post/1398216/


All Articles