338

How do I get the computer name in .NET c#

6
  • 2
    Duplicate Question link
    – Malachi
    Apr 18, 2013 at 15:57
  • 1
    @Malachi, that question is about Windows services.
    – Sam
    Jun 13, 2013 at 2:28
  • 2
    @Sam a windows service is just a windows application that runs in the background, so really it's the same thing.
    – Malachi
    Jun 13, 2013 at 13:04
  • 1
    @Malachi, yeah, I know what you mean. However, the I think the references to the ASP.NET-specific and Winforms-specific ways of doing this in the answers to this question mightn't apply in that question.
    – Sam
    Jun 15, 2013 at 0:14
  • 1
    @Malachi, I think you've misunderstood what I meant about ASP.NET. I wasn't referring to an ASP.NET application getting its clients' computer names, although doing so (for the DNS name) is probably easy since the application would get the clients' IP addresses. I was referring to ASP.NET applications getting their host computers' names. See the highest-rated answer here for an example.
    – Sam
    Jun 17, 2013 at 22:42

12 Answers 12

475

See How to find FQDN of local machine in C#/.NET ? if the last doesn't give you the FQDN and you need it.

See details about Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName

10
  • 3
    @tvanfosson: what is the difference ? I mean, which one should I use when? Does this have any security implications?
    – P.K
    Nov 20, 2009 at 4:26
  • 28
    I would use the first from a console or winforms app, the second from a web app, and the third if I needed to get the FQDN. See the referenced documentation for information on the permissions required.
    – tvanfosson
    Nov 20, 2009 at 11:49
  • 1
    @tvanfosson All of these three get me the server name and not the clients machine which is accessing the page. Is this how it works? or am I doing something incorrectly. I would like to get the clients machine, and not the server. I am using Web Forms with asp.NET 4.0
    – Ryan S
    Jun 13, 2012 at 12:41
  • 2
    I just tested this and found that Dns.GetHostName() didn't give the fully-qualified name.
    – Sam
    Jun 13, 2013 at 4:33
  • 6
    Note that System.Enviornment.MachineName is only going to give you the NetBIOS name, so if the host name is longer than 15 characters, you'll hit problems if you need the full name. I can't speak for the others. Jan 27, 2014 at 18:38
92

System.Environment.MachineName

Or, if you are using Winforms, you can use System.Windows.Forms.SystemInformation.ComputerName, which returns exactly the same value as System.Environment.MachineName.

60
System.Environment.MachineName
1
  • As easy as pie in F# open system ... let system_name = System.Environment.MachineName Nov 3, 2016 at 19:31
38

Some methods are given below to get machine name or computer name

Method 1:-

string MachineName1 = Environment.MachineName;

Method 2:-

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

Method 3:-

string MachineName3 = Request.ServerVariables["REMOTE_HOST"].ToString();

Method 4:-

string MachineName4 = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

For more see my blog

2
  • When the Hostname is longer than 15 characters, this methods will return different names! Dec 2, 2020 at 6:11
  • 1
    The blog link doesn't give any more information about the API mentioned in this answer. So I don't think it should be added here.
    – MartinZ
    May 11, 2021 at 2:50
24
string name = System.Environment.MachineName;
16

Well there is one more way: Windows Management Instrumentation

using System.Management;

try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT Name FROM Win32_ComputerSystem");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_ComputerSystem instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
        }
        catch (ManagementException e)
        {
            // exception handling
        }

MSDN

WMI

WMI Code creator

FAQs

0
16

You can have access of the machine name using Environment.MachineName.

4

Try this:

string[] computer_name = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
return computer_name[0].ToString();
1
  • 1
    DNS name and computername may differ. He's asking for the computername! Oct 4, 2015 at 10:45
4

I set the .InnerHtml of a <p> bracket for my web project to the user's computer name doing the following:

HTML:

    <div class="col-md-4">
       <h2>Your Computer Name Is</h2>
       <p id="pcname" runat="server"></p>
       <p>
           <a class="btn btn-default" href="#">Learn more &raquo;</a>
       </p>
    </div>

C#:

using System;
using System.Web.UI;

namespace GetPCName {
   public partial class _Default : Page {
    protected void Page_Load(object sender, EventArgs e) {            
        pcname.InnerHtml = Environment.MachineName;
    }
   }
}
3

Make it simple by using this one line

Environment.MachineName;
1

2 more helpful methods: System.Environment.GetEnvironmentVariable("ComputerName" )

System.Environment.GetEnvironmentVariable("ClientName" ) to get the name of the user's PC if they're connected via Citrix XenApp or Terminal Services (aka RDS, RDP, Remote Desktop)

1

Try this one.

public static string GetFQDN()
{
    string domainName = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
    string hostName = Dns.GetHostName();
    string fqdn;
    if (!hostName.Contains(domainName))
        fqdn = hostName + "." +domainName;
    else
        fqdn = hostName;

    return fqdn;
}
1
  • 1
    But why not use System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().HostName?
    – osexpert
    May 15, 2018 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy