Search code examples
c#workgroup

How can I get the Workgroup name of a computer using C#?


I've read about getting it with the Environment class, but can't find it.

Thanks guys.


Solution

  • You can do this with WMI; add a reference to System.Management.dll and a using statement for System.Management namespace, then call the following code:

    ManagementObjectSearcher mos = 
      new ManagementObjectSearcher(@"root\CIMV2", @"SELECT * FROM Win32_ComputerSystem");
    foreach (ManagementObject mo in mos.Get()) {
      Console.WriteLine(mo["Workgroup"]);
    }