How do I get the local machine name in C#?

后端 未结 5 848
梦谈多话
梦谈多话 2020-11-27 18:27

How do I get the local machine name?

相关标签:
5条回答
  • 2020-11-27 19:12

    From link text

    Four ways to get your local network/machine name:

    string name = Environment.MachineName;
    string name = System.Net.Dns.GetHostName();
    string name = System.Windows.Forms.SystemInformation.ComputerName;
    string name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
    

    More information at: Difference between SystemInformation.ComputerName, Environment.MachineName, and Net.Dns.GetHostName

    0 讨论(0)
  • 2020-11-27 19:18

    You should be able to use System.Environment.MachineName for this. It is a property that returns a string containing the netBIOS name of the computer:

    http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx

    0 讨论(0)
  • 2020-11-27 19:29

    My computer name is more than 15 chars, so i use hostname.exe to get full length name:

    Process proc = new Process();
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.FileName = "c:/windows/system32/hostname.exe";
    proc.Start();
    var hostName = proc.StandardOutput.ReadLine();
    
    0 讨论(0)
  • 2020-11-27 19:31

    If you want the FQDN (fully qualified domain name) of the local computer, you can use

    System.Net.Dns.GetHostEntry("localhost").HostName

    The other methods will only return the local name, without any domain specific info. For instance, for the computer myComp.myDomain.com, the previous methods will return myComp, whereas the GetHostEntry method will return myComp.myDomain.com

    0 讨论(0)
  • 2020-11-27 19:32

    System.Environment.MachineName

    It works unless a machine name has more than 15 characters.

    0 讨论(0)
提交回复
热议问题