Retrieving Computer Name in .NET
In the .NET programming framework, there are several methods to retrieve the name of the computer on which an application is running.
Console or WinForms Application
For console or WinForms applications, you can use the System.Environment.MachineName property:
string computerName = System.Environment.MachineName;
Web Application
For web applications, you can access the computer name through the HttpContext.Current.Server object:
string computerName = HttpContext.Current.Server.MachineName;
Fully Qualified Domain Name (FQDN)
If you need the fully qualified domain name (FQDN) instead of the short name, use the System.Net.Dns.GetHostName() method:
string fqdn = System.Net.Dns.GetHostName();
Additional Considerations
FQDN Resolution:
If System.Net.Dns.GetHostName() does not return the FQDN, refer to the question "How to find FQDN of local machine in C#/.NET?" for additional details.
Property Differences:
For more information on the differences between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName, refer to the documentation "Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName".
The above is the detailed content of How to Retrieve the Computer Name in .NET?. For more information, please follow other related articles on the PHP Chinese website!