How do I detect whether the machine is joined to an Active Directory domain (versus in Workgroup mode)?
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);
}
}