
How do I get the computer name in .NET c#
713 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 remaining does not give you the FQDN and you want it.
See details about Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName
10System.Environment.GadgetName
Or, if you're the use of Winforms, you'll be able to use System.Windows.Forms.SystemData.ComputerName
, which returns exactly the same price as System.Environment.SystemName
.
System.Environment.MachineName
1Some methods are given underneath to get gadget name or computer name
Method 1:-
string MachineName1 = Environment.GadgetName;
Method 2:-
string DeviceName2 = System.Net.Dns.GetHostName();
Method 3:-
string GadgetName3 = Request.ServerVariables["REMOTE_HOST"].ToString();
Method 4:-
string SystemName4 = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
For extra see my blog
2string name = System.Environment.GadgetName;
Well there is yet one more method: Windows Management Instrumentation
the use of System.Management; take a look at 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
0You will have get entry to of the gadget name the use of Environment.MachineName.
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();
1I set the .InnerHtml of a <p>
bracket for my web project to the user's computer name doing the following:
HTML:
<div elegance="col-md-4"> <h2>Your Computer Name Is</h2> <p identity="pcname" runat="server"></p> <p> <a category="btn btn-default" href="#">Learn more »</a> </p> </div>
C#:
the usage of System; the usage of System.Web.UI; namespace GetPCName public partial elegance _Default : Page protected void Page_Load(object sender, EventArgs e) pcname.InnerHtml = Environment.SystemName;
Make it simple by way of using this one line
Environment.MachineName;
2 extra useful strategies: System.Environment.GetEnvironmentVariable("ComputerName" )
System.Environment.GetEnvironmentVariable("ClientName" ) to get the name of the person's PC if they're hooked up by means of Citrix XenApp or Terminal Services (aka RDS, RDP, Remote Desktop)
Try this one.
public static string GetFQDN() string areaName = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().AreaName; string hostName = Dns.GetHostName(); string fqdn; if (!hostName.Contains(areaName)) fqdn = hostName + "." +areaName; else fqdn = hostName; return fqdn;
1fyi: Environment.SystemName and System.Windows.Forms.SystemData.ComputerName only go back 15 characters. Use System.Net.Dns.GetHostName() to get full name.
1ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaHBuaGaGeXvHqK5mnJ9itm6zxK1kraCVYrCwuc%2Buq56qXaOurrGMoqVmppWp