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?
Asked
Active
Viewed 6,178 times
3 Answers
6
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).

Community
- 1
- 1

Christian.K
- 47,778
- 10
- 99
- 143
-
Do you have any authoritative source that no hostnames can contain a dot? – Louis Rhys Feb 26 '12 at 16:32
5
Out of a fqdn:
string s = "some.computer.name";
string host = s.Substring(0, s.IndexOf('.'));
Out of the framework:
System.Net.Dns.GetHostName();

StaWho
- 2,488
- 17
- 24
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];

Waleed.alhasan
- 89
- 2
- 6