Get computer name

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
 
A

Arne Schittenhelm

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.
 
T

Thomas P. Skinner [MVP]

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

Thomas P. Skinner [MVP]
 
W

Willy Denoyette [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.
 
G

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top