Using C#, what is the way to find out if the machine my code is running on IS a domain controller.
I do not want to "go out" and collect information about any other domain controllers on the domain... I am ONLY interested if the machine machine my code is running on IS a domain controller or not. Additionally, I do not need to know if its a primary domain controller ... just if it IS a domain controller.
Here is what I have tried
TRIAL 1
private bool IsDomainController()
{
Domain domain = Domain.GetCurrentDomain();
string domainName = domain.ToString();
bool bIsDC = false;
DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Domain, domainName);
try
{
using (DomainController dc = DomainController.FindOne(ctx, LocatorOptions.ForceRediscovery))
{
bIsDC = true;
}
}
catch (Exception)
{
bIsDC = false;
}
return bIsDC;
}
and
TRIAL 2
public bool IsThisMachineIsADomainController()
{
Domain domain = Domain.GetCurrentDomain();
string thisMachine = String.Format("{0}.{1}", Environment.MachineName, domain.ToString());
thisMachine = thisMachine.ToLower();
//Enumerate Domain Controllers
List<string> allDcs = new List<string>();
string name = "";
foreach (DomainController dc in domain.DomainControllers)
{
name = dc.Name.ToLower();
allDcs.Add(name);
}
return allDcs.Contains(thisMachine);
}
Are either of these suitable and why or why not?