Get computer name

  • Thread starter Thread starter Gonçalo Boléo
  • Start date Start date
G

Gonçalo Boléo

How can i get the name of my computer?
Is there any class that gives me access to properties like ComputerName,
Workgroup, Domain?

thnaks,
Gonçalo Boléo
 
How can i get the name of my computer?
Is there any class that gives me access to properties like ComputerName,
Workgroup, Domain?

thnaks,
Gonçalo Boléo

Hello Goncalo,

Try this.

using System.Security.Principal;
WindowsIdentity.GetCurrent().Name.ToString();

This returns a string computer/username.
 
Also check out the System.Windows.Forms.SystemInformation class. It has a
static property that returns the computer name.

Thomas P. Skinner [MVP]
 
Using System.Management and WMI (WinMe, W2K, XP and W2K3).

ManagementObject cs;
using(cs = new ManagementObject ("Win32_ComputerSystem.Name='"+
System.Environment.MachineName + "'"))
{
cs.Get();
if ((bool)cs["partOfDomain"] != null)
{
if ((bool)cs["partOfDomain"] == true)
{
Console.WriteLine("Domain: {0}",cs["domain"]);
}
else
Console.WriteLine("Workgroup: {0}",cs["workgroup"]);
}
else
Console.WriteLine("No domain/Workgrup member");
}

Willy.
 
(1) // Gets the string containing the DNS host name of the local computer.
String hostName = Dns.GetHostName();
(2) // Gets the NetBIOS name of this local computer
String hostName = Environment.MachineName;
 
Back
Top