How to detect if machine is joined to domain?

后端 未结 12 1983
梦毁少年i
梦毁少年i 2020-11-28 08:59

How do I detect whether the machine is joined to an Active Directory domain (versus in Workgroup mode)?

12条回答
  •  醉话见心
    2020-11-28 09:29

    You can check the PartOfDomain property of Win32_ComputerSystem WMI class. The MSDN says :

    PartOfDomain

    Data type: boolean

    Access type: Read-only

    If True, the computer is part of a domain. If the value is NULL, the computer is not in a domain or the status is unknown. If you unjoin the computer from a domain, the value becomes false.

    /// 
    /// Determines whether the local machine is a member of a domain.
    /// 
    /// A boolean value that indicated whether the local machine is a member of a domain.
    /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102%28v=vs.85%29.aspx
    public bool IsDomainMember()
    {
        ManagementObject ComputerSystem;
        using (ComputerSystem = new ManagementObject(String.Format("Win32_ComputerSystem.Name='{0}'", Environment.MachineName)))
        {
            ComputerSystem.Get();
            object Result = ComputerSystem["PartOfDomain"];
            return (Result != null && (bool)Result);
        }
    }   
    

提交回复
热议问题