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)
Click "System" in the menu. Within the "System" menu, click "Change Settings." On the "Computer Name" tab, click "Change." Choose "Workgroup" instead of "Domain," and type the name of a new or existing work group. Click "OK," and restart the computer for the changes to take effect.
Click on the Start button. In the search box, type Computer. Right click on This PC within the search results and select Properties. Under Computer name, domain, and workgroup settings you will find the computer name listed.
Click the Windows Start button, then "All Programs" and "Accessories." Right-click on "Command Prompt" and choose "Run as Administrator." Type "nslookup %ipaddress%" in the black box that appears on the screen, substituting %ipaddress% with the IP address for which you want to find the hostname.
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).
Out of a fqdn:
string s = "some.computer.name";
string host = s.Substring(0, s.IndexOf('.'));
Out of the framework:
System.Net.Dns.GetHostName();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With