Home >Backend Development >C++ >How to Get a Computer's Name and FQDN in .NET?

How to Get a Computer's Name and FQDN in .NET?

Susan Sarandon
Susan SarandonOriginal
2025-01-04 10:40:35737browse

How to Get a Computer's Name and FQDN in .NET?

Retrieving the Computer Name in .NET

Obtaining the computer name is a common operation in .NET applications. Depending on the application type, you can use different techniques to retrieve this information.

Console or WinForms Applications:

For console applications or Windows Forms applications, you can use the System.Environment.MachineName property, which returns the NETBIOS computer name. The code below demonstrates its usage:

string computerName = System.Environment.MachineName;

Web Applications:

In ASP.NET web applications, you can access the computer name through the HttpContext.Current.Server.MachineName property. This property retrieves the server name from the request context.

string computerName = HttpContext.Current.Server.MachineName;

Fully Qualified Domain Name (FQDN):

If you need to obtain the fully qualified domain name (FQDN) of the computer, you can use the System.Net.Dns.GetHostName() method.

string fqdn = System.Net.Dns.GetHostName();

Additional Considerations:

  • If System.Net.Dns.GetHostName() does not provide the FQDN, you can refer to the article "How to find FQDN of local machine in C#/.NET?" for alternative approaches.
  • For detailed information on the differences between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName, please see the provided link.

The above is the detailed content of How to Get a Computer's Name and FQDN in .NET?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn