How do I get the computer name in .NET

C#.Net

C# Problem Overview


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

C# Solutions


Solution 1 - C#

Solution 2 - C#

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.

Solution 3 - C#

System.Environment.MachineName

Solution 4 - C#

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

Solution 5 - C#

string name = System.Environment.MachineName;

Solution 6 - C#

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

Solution 7 - C#

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

Solution 8 - C#

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();

Solution 9 - C#

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;
    }
   }
}

Solution 10 - C#

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)

Solution 11 - C#

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;
}

Solution 12 - C#

Make it simple by using this one line

Environment.MachineName;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser215174View Question on Stackoverflow
Solution 1 - C#tvanfossonView Answer on Stackoverflow
Solution 2 - C#Zach JohnsonView Answer on Stackoverflow
Solution 3 - C#AlteredConceptView Answer on Stackoverflow
Solution 4 - C#NEERAJ SRIVASTAVAView Answer on Stackoverflow
Solution 5 - C#ZachView Answer on Stackoverflow
Solution 6 - C#CesarGonView Answer on Stackoverflow
Solution 7 - C#PRRView Answer on Stackoverflow
Solution 8 - C#A GhazalView Answer on Stackoverflow
Solution 9 - C#JoeView Answer on Stackoverflow
Solution 10 - C#JRrelyeaView Answer on Stackoverflow
Solution 11 - C#LEFOPHANA GODFREYView Answer on Stackoverflow
Solution 12 - C#Ramgy BorjaView Answer on Stackoverflow