Get Computer's Workgroup Name RRS feed

  • Question

  • Get Computer's Workgroup Name?

    I have been searching the web a bit to find out how to do this, but so far a blank.

    Please comment.

    Wednesday, February 26, 2020 6:57 PM

All replies

  • A way is with NetGetJoinInformation (P/Invoke)

    • Proposed as answer by simonb549 Thursday, February 27, 2020 7:50 AM
    Wednesday, February 26, 2020 7:19 PM
  • Hi Jack0987,
    Please try this snippet code to getting WorkGroup name, once you should install system.Management Nuget package into the solutions project  

    using System;
    using System.Management;
    namespace GettingWorkgrupName
        class Program
            static void Main(string[] args)
            public static string GettingMyWorkGroup()
                ManagementObject computer_system = new ManagementObject(string.Format("Win32_ComputerSystem.Name='{0}'",Environment.MachineName));
                object result = computer_system["Workgroup"];
                return result.ToString();
            public static void GettingMyWorkGroup2()
                ManagementObjectSearcher mos = new ManagementObjectSearcher(@"root\CIMV2", @"SELECT * FROM Win32_ComputerSystem");
                foreach (ManagementObject mo in mos.Get())

    Wednesday, February 26, 2020 7:23 PM
  • Is the workgroup name in the registry?

    Wednesday, February 26, 2020 9:12 PM
  • Hi jack,

    Thank you for posting here.

    The easiest way to get a workgroup is to use WMI, the method provided by Rebin.

    But some people say that WMI will be a bit slow in some cases, so using NetGetJoinInformation may be a better choice, but Andrew's code is missing an enum, adding it to the code and it will work properly.

        static class DomainInformation
            static extern int NetApiBufferFree(IntPtr Buffer);
            [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            static extern int NetGetJoinInformation(
              string server,
              out IntPtr domain,
              out NetJoinStatus status);
            // Win32 Result Code Constant
            const int ErrorSuccess = 0;
            public enum NetJoinStatus
                NetSetupUnknownStatus = 0,
            // Returns the domain name the computer is joined to, or "" if not joined.
            public static string GetDomainOrWorkgroup()
                int result = 0;
                string domain = null;
                IntPtr pDomain = IntPtr.Zero;
                NetJoinStatus status = NetJoinStatus.NetSetupUnknownStatus;
                    result = NetGetJoinInformation(null, out pDomain, out status);
                    if (result == ErrorSuccess)
                        switch (status)
                            case NetJoinStatus.NetSetupDomainName:
                            case NetJoinStatus.NetSetupWorkgroupName:
                                domain = Marshal.PtrToStringAuto(pDomain);
                    if (pDomain != IntPtr.Zero)
                if (domain == null) domain = "";
                return domain;

    Hope this could be helpful.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Rebin Qadir Friday, February 28, 2020 8:55 AM
    Thursday, February 27, 2020 3:00 AM