开发者

How do I get the computer name in .NET

开发者 https://www.devze.com 2022-12-12 04:45 出处:网络
How do I get the computer name 开发者_运维问答in .NET c# System.Environment.MachineName from a console or WinForms app.

How do I get the computer name 开发者_运维问答in .NET c#


  • System.Environment.MachineName from a console or WinForms app.
  • HttpContext.Current.Server.MachineName from a web app
  • System.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


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.


System.Environment.MachineName


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


string name = System.Environment.MachineName;


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


You can have access of the machine name using 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();


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


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)


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


Make it simple by using this one line

Environment.MachineName;
0

精彩评论

暂无评论...
验证码 换一张
取 消