How do I know if my program works on a domain controller?

With C #, is there a way to find out if the computer my program is running on is a domain controller?

+3
source share
3 answers

Enumerate DCs (fragment below here ) - check the name of your server 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;
}
+1
source

A 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 ", this is the client machine. If it is ServerNT , the server and LanmanNT are domain controllers.

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

+2

, , .

    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
source

Source: https://habr.com/ru/post/1767252/


All Articles