开发者

How can I tell if my program is running on a Domain Controller?

Using C# is there a way to tell if the computer my program is running on is a Domain Controller?开发者_StackOverflow


Shorter solution, just check the registry:

const string basename = "HKEY_LOCAL_MACHINE\\";    
const string keyname = "SYSTEM\\CurrentControlSet\\Control\\ProductOptions";
string result = (string) Registry.GetValue(basename + keyname, "ProductType", "WinNT");

If the result is "WinNT" it's a client machine. If it's "ServerNT", server, and "LanmanNT" is a Domain Controller.

More info: https://technet.microsoft.com/en-us/library/cc782360%28v=ws.10%29.aspx


Enumerate DCs (snippet below from here) - check for your server name in the resulting list:

public static ArrayList EnumerateDomainControllers()
{
    ArrayList alDcs = new ArrayList();
    Domain domain = Domain.GetCurrentDomain();
    foreach (DomainController dc in domain.DomainControllers)
    {
        alDcs.Add(dc.Name);
    }
    return alDcs;
}


Here's a complete function I came up with, to answer the question.

    public static bool ThisMachineIsADomainController()
    {
        Domain domain = Domain.GetCurrentDomain();

        string thisMachine = String.Format("{0}.{1}",Environment.MachineName, domain.ToString());
        //Enumerate Domain Controllers
        List<string> allDcs = new List<string>();

        foreach (DomainController dc in domain.DomainControllers)
        {
            allDcs.Add(dc.Name);
        }
        return allDcs.Contains(thisMachine);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜