Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the local machine name in C#?

Tags:

c#

hostname

dns

How do I get the local machine name?

like image 896
Yoann. B Avatar asked Mar 19 '09 13:03

Yoann. B


People also ask

How to get local hostname in C?

In C, you may use the gethostname function. #include <unistd. h> int gethostname(char *name, size_t namelen); The gethostname() function shall return the standard host name for the current machine.

How do I print a hostname?

Using the command promptFrom the Start menu, select All Programs or Programs, then Accessories, and then Command Prompt. In the window that opens, at the prompt, enter hostname . The result on the next line of the command prompt window will display the hostname of the machine without the domain.

Is the hostname the machine name?

Machine Name is the NetBIOS name of your computer and is used by the Windows Browser Service. The Hostname is the Fully Qualified Domain Name (FQDN) used by DNS. If you\'re on a domain, your Hostname may look something like this: MyComputerName.TheDomainName.com. This is quite common and is normal.


2 Answers

System.Environment.MachineName

It works unless a machine name has more than 15 characters.

like image 112
annakata Avatar answered Sep 23 '22 19:09

annakata


From source

Four ways to get your local network/machine name:

string name = Environment.MachineName; string name = System.Net.Dns.GetHostName(); string name = System.Windows.Forms.SystemInformation.ComputerName; string name = System.Environment.GetEnvironmentVariable("COMPUTERNAME"); 

More information at: Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName

like image 29
Steve Avatar answered Sep 26 '22 19:09

Steve