简体   繁体   English

如何判断我的程序是否在域控制器上运行?

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

使用C#有一种方法可以判断我的程序运行的计算机是否是域控制器?

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. 如果结果是“ WinNT ”,那么它就是客户端机器。 If it's " ServerNT ", server, and " LanmanNT " is a Domain Controller. 如果是“ ServerNT ”,则服务器和“ LanmanNT ”是域控制器。

More info: https://technet.microsoft.com/en-us/library/cc782360%28v=ws.10%29.aspx 更多信息: 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: 枚举DC( 此处的代码段) - 在结果列表中检查您的服务器名称:

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么知道我的程序在单元测试环境下 - How can I tell that my program is under unit test environment 如何判断我的程序是否专注于那个程序? - How can I tell if my program is the one that's focused? 如何判断我的进程是否以管理员身份运行? - How can I tell if my process is running as Administrator? 如何判断我的代码是否正在异步运行? - How can I tell if my code is running async? 如何告诉我的方法忽略异常并继续运行? - How can I tell my method to ignore an exception and continue running? 我可以打电话告诉我我的程序是编译器吗 - Can I call tell that my program is compiler 如何判断我的程序所运行的计算机是否使用超线程? (C#) - How do I tell if the machine my program is running on uses hyperthreading? (C#) 如何检查我的程序是否真的以管理员​​身份运行? - How can I check if my program is really running as an administrator? 如何确定我的桌面程序是否正在控制台上运行? - How can I find out if my desktop program is running on the console? 如何使用此堆栈跟踪来判断是什么导致控制器的动作中断? - How can I use this stack trace to tell what is causing my controller's action to break?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM