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

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

Thanks guys.

+3
source share
5 answers

You can do this using WMI; add a link to System.Management.dll and a using statement for the 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"]);
}
+4
source

A method based on Jono's 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();
}
+6
source

, WMI, ( ) ( 5 ). API- "NetGetJoinInformation" (PInvoke.net). API- , .

+3

. P/Invoke.

0

, , :

System.Environment.GetEnvironmentVariable("USERDOMAIN")

. ( "set" , ).

0

Source: https://habr.com/ru/post/1742671/


All Articles