Question

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

Thanks guys.

Was it helpful?

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"]);
}

OTHER TIPS

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top