You can run PInvoke for the Win32 API, such as NetGetDcName, which will return a blank / empty string for a computer that is not joined to the domain.
Even better is NetGetJoinInformation, which will tell you explicitly if the machine is not connected, in a workgroup or in a domain.
Using NetGetJoinInformation I put together this and it worked for me:
public class Test { public static bool IsInDomain() { Win32.NetJoinStatus status = Win32.NetJoinStatus.NetSetupUnknownStatus; IntPtr pDomain = IntPtr.Zero; int result = Win32.NetGetJoinInformation(null, out pDomain, out status); if (pDomain != IntPtr.Zero) { Win32.NetApiBufferFree(pDomain); } if (result == Win32.ErrorSuccess) { return status == Win32.NetJoinStatus.NetSetupDomainName; } else { throw new Exception("Domain Info Get Failed", new Win32Exception()); } } } internal class Win32 { public const int ErrorSuccess = 0; [DllImport("Netapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)] public static extern int NetGetJoinInformation(string server, out IntPtr domain, out NetJoinStatus status); [DllImport("Netapi32.dll")] public static extern int NetApiBufferFree(IntPtr Buffer); public enum NetJoinStatus { NetSetupUnknownStatus = 0, NetSetupUnjoined, NetSetupWorkgroupName, NetSetupDomainName } }
Rob May 29 '09 at 2:40 p.m. 2009-05-29 14:40
source share