How do I get the computer name in .NET c#
-
2Duplicate Question [link](http://stackoverflow.com/questions/459034/get-computer-name-from-within-a-windows-service) – Malachi Apr 18 '13 at 15:57
-
1@Malachi, that question is about Windows services. – Sam Jun 13 '13 at 02: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 '13 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 '13 at 00:14
-
@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 – Malachi Jun 17 '13 at 20:52
-
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 '13 at 22:42
-
Does this answer your question? [How do I get the local machine name in C#?](https://stackoverflow.com/questions/662282/how-do-i-get-the-local-machine-name-in-c) – Rand Random Jun 27 '23 at 13:34
13 Answers
System.Environment.MachineName
from a console or WinForms app.HttpContext.Current.Server.MachineName
from a web appSystem.Net.Dns.GetHostName()
to get the FQDN
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

- 1
- 1

- 524,688
- 99
- 697
- 795
-
3@tvanfosson: what is the difference ? I mean, which one should I use when? Does this have any security implications? – P.K Nov 20 '09 at 04:26
-
28I 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 '09 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 '12 at 12:41
-
@RyanSammut - you can get the hostname and IP address pretty easily (if you're not behind a load balancer) by checking Request.UserHostName and Request.UserIPAddress. Note, I'm not claiming they will be accurate or correspond to what the user thinks their hostname/ip address is - could be their ISP, a TOR end point, ... - but it might suffice for your purposes. You may also need to configure IIS to resolve DNS names for every request to use the host name, not sure about that. IP address is all I've ever actually used from the request. – tvanfosson Jun 13 '12 at 12:49
-
3I just tested this and found that `Dns.GetHostName()` **didn't** give the fully-qualified name. – Sam Jun 13 '13 at 04:33
-
@Sam - I'm pretty sure that if it is part of a domain that the DNS resolver will return the FQDN. If not, you might need to do a reverse look up on the IP addresses. If you're behind a router doing NAT, you might be out of luck since the FQDN you're probably interested in is the router's. – tvanfosson Jun 13 '13 at 13:18
-
@tvanfosson, in my case, the computer is part of a domain, but it didn't work. Doing a reverse look-up on the IP address does work, as you suggested. However, the solutions in [the question on FQDNs](http://stackoverflow.com/q/804700/238753) also work for me. In particular, see [my answer](http://stackoverflow.com/a/17079669/238753), which is similar to your technique except it works in my case. – Sam Jun 13 '13 at 22:55
-
7Note 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. – RandomInsano Jan 27 '14 at 18:38
-
If performance is a concern to you, I found that System.Environment.MachineName was basically free (0ms) where the Dns.GetHostName() method took 333ms. – BearsEars Jun 06 '19 at 19:04
-
Dns.GetHostName() does _not_ provide the FQDN. Even the linked answer implies that it does _not_ contain the full qualification. – user2864740 Apr 15 '20 at 02:31
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
.

- 23,678
- 6
- 69
- 86
System.Environment.MachineName

- 12,769
- 10
- 63
- 83

- 2,602
- 7
- 32
- 36
-
As easy as pie in F# `open system` ... `let system_name = System.Environment.MachineName` – octopusgrabbus Nov 03 '16 at 19:31
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

- 134,786
- 31
- 255
- 325

- 429
- 4
- 4
-
When the Hostname is longer than 15 characters, this methods will return different names! – marsh-wiggle Dec 02 '20 at 06:11
-
1The 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 '21 at 02:50
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
}

- 1,190
- 7
- 13
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();

- 97,193
- 102
- 206
- 364

- 2,693
- 1
- 19
- 12
-
1DNS name and computername may differ. He's asking for the computername! – marsh-wiggle Oct 04 '15 at 10:45
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 »</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;
}
}
}

- 37
- 8
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)

- 139
- 1
- 4
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;
}

- 60,010
- 15
- 145
- 220

- 11
- 1
-
1But why not use System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().HostName? – osexpert May 15 '18 at 09:48
fyi: Environment.MachineName and System.Windows.Forms.SystemInformation.ComputerName only return 15 characters. Use System.Net.Dns.GetHostName() to get full name.

- 11