How do I get the computer name in .NET c#
-
2Duplicate Question linkMalachi– Malachi2013-04-18 15:57:01 +00:00Commented Apr 18, 2013 at 15:57
-
1@Malachi, that question is about Windows services.Sam– Sam2013-06-13 02:28:03 +00:00Commented 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.Malachi– Malachi2013-06-13 13:04:55 +00:00Commented 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– Sam2013-06-15 00:14:52 +00:00Commented 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 FrameworkMalachi– Malachi2013-06-17 20:52:08 +00:00Commented Jun 17, 2013 at 20:52
13 Answers
System.Environment.MachineNamefrom a console or WinForms app.HttpContext.Current.Server.MachineNamefrom 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
11 Comments
Dns.GetHostName() didn't give the fully-qualified name.System.Environment.MachineName
1 Comment
open system ... let system_name = System.Environment.MachineNameSome 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
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
}
Comments
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
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;
}
}
}
Comments
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
fyi: Environment.MachineName and System.Windows.Forms.SystemInformation.ComputerName only return 15 characters. Use System.Net.Dns.GetHostName() to get full name.