本文介绍了如何检测,如果计算机加入到域(C#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何检测机器是否被加入到Active Directory域(而不是在工作组模式)?
How do I detect whether the machine is joined to an Active Directory domain (versus in Workgroup mode)?
推荐答案
您可以的PInvoke到Win32 API中的如的它会返回一个空/空字符串的非加入域的计算机。
You can PInvoke to Win32 API's such as NetGetDcName which will return a null/empty string for a non domain-joined machine.
更妙的是 NetGetJoinInformation 它会告诉你明确的,如果一台机器是未连接在工作组或域。
Even better is NetGetJoinInformation which will tell you explicitly if a machine is unjoined, in a workgroup or in a domain.
使用 NetGetJoinInformation
我放在一起这一点,这为我工作:
Using NetGetJoinInformation
I put together this, which 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)
{
NetApiBufferFree(pDomain);
}
if (result == Win32.ErrorSuccess)
{
return status == Win32.NetJoinStatus.NetSetupDomainName;
}
else
{
throw new Exception("Domain Info Get Failed");
}
}
}
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
}
}
这篇关于如何检测,如果计算机加入到域(C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!