4

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

Thanks guys.

Sampson
  • 265,109
  • 74
  • 539
  • 565

4 Answers4

7

A way based on the Jono response, but shorter:

public static string GetWorkGroup()
{
    ManagementObject computer_system = new ManagementObject(
                string.Format(
                "Win32_ComputerSystem.Name='{0}'",
                Environment.MachineName));

    object result = computer_system["Workgroup"];
    return result.ToString();
}
JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50
5

I tried this using the WMI options suggested here, but it turned out to be excruciatingly slow (sometimes over 5 seconds) on my machine (and several others in my office). What ended up working for me was using the API call "NetGetJoinInformation" (PInvoke.net). The API call returns very quickly for me and does exactly what I need.

Bryan Bosley
  • 131
  • 2
  • 2
4

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"]);
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Jono
  • 1,964
  • 4
  • 18
  • 35
  • Thanks for the edit, Hans. At first it had the escaped backslash, then I edited it to make them verbatim strings but the syntax highlighting disappeared. When I changed it back (I like seeing my code in color,) I didn't get it quite right. Still no highlighting though, I see? – Jono Apr 25 '10 at 16:36
0

Look here for an example. You will have to use P/Invoke.

n535
  • 4,983
  • 4
  • 23
  • 28