365

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

7
  • 2
    Duplicate Question link Commented Apr 18, 2013 at 15:57
  • 1
    @Malachi, that question is about Windows services. Commented Jun 13, 2013 at 2:28
  • 3
    @Sam a windows service is just a windows application that runs in the background, so really it's the same thing. Commented 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. Commented Jun 15, 2013 at 0:14
  • 1
    @Sam all this question asks is how to get the computer name. you can't really do that over the internet. .NET is a Framework. there is almost no Difference between a Windows service and a Windows Form Application. other than a windows service happens behind the scenes without a user interface. taking information from a user's computer over the internet is not an easy thing to do. neither Question asks how to get the user's computer information over the internet. and neither question mentions ASP.NET which is a subset of the .NET Framework Commented Jun 17, 2013 at 20:52

13 Answers 13

510

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

Sign up to request clarification or add additional context in comments.

11 Comments

@tvanfosson: what is the difference ? I mean, which one should I use when? Does this have any security implications?
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 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
I just tested this and found that Dns.GetHostName() didn't give the fully-qualified name.
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.
|
100

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.

Comments

64
System.Environment.MachineName

1 Comment

As easy as pie in F# open system ... let system_name = System.Environment.MachineName
46

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 Comments

When the Hostname is longer than 15 characters, this methods will return different names!
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.
25
string name = System.Environment.MachineName;

Comments

17

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

Comments

16

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

Comments

5

Make it simple by using this one line

Environment.MachineName;

Comments

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 Comment

DNS name and computername may differ. He's asking for the computername!
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;
    }
   }
}

Comments

2

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)

Comments

2

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 Comment

But why not use System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().HostName?
1

fyi: Environment.MachineName and System.Windows.Forms.SystemInformation.ComputerName only return 15 characters. Use System.Net.Dns.GetHostName() to get full name.

1 Comment

This is commentary on existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.