How to get the computer name?

0

How can I get "computer name" and "Full computer name" using C #?

What are shown in the properties of the computer:

  

    
asked by anonymous 26.06.2018 / 16:50

4 answers

5

What you refer to as the full name is actually the MachineName and the HostName name. You can do it this way:

static void Main(string[] args)
{
    var nome = Environment.MachineName;
    var nomeCompleto = Dns.GetHostEntry(nome).HostName;

    Console.WriteLine(nome); //PC-162
    Console.WriteLine(nomeCompleto); //PC-162.meudominio.com.br

    Console.Read();
}
    
26.06.2018 / 17:32
1

To get the computer name just use the class Environment

static void Main(string[] args)
{
    var name = Environment.MachineName;
    Console.WriteLine("Nome do computador: {0}", name);
    Console.ReadLine();
}
    
26.06.2018 / 16:54
1

To get the name of the machine use Environment.MachineName :

class Sample 
{
    public static void Main() 
    {
        Console.WriteLine("Nome: {0}", Environment.MachineName);
    }
}

Link: link

    
26.06.2018 / 16:54
0

I think it's using the "Environment.MachineName", but I can not confirm because I'm trying to get the same as you

    
26.06.2018 / 17:26