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



Question:

How do I get the local machine name?


Solution:1

System.Environment.MachineName


Solution:2

You should be able to use System.Environment.MachineName for this. It is a property that returns a string containing the netBIOS name of the computer:

http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx


Solution:3

From link text

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


Solution:4

If you want the FQDN (fully qualified domain name) of the local computer, you can use

System.Net.Dns.GetHostEntry("localhost").HostName

The other methods will only return the local name, without any domain specific info. For instance, for the computer myComp.myDomain.com, the previous methods will return myComp, whereas the GetHostEntry method will return myComp.myDomain.com


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »