如何获得域控制器的时间

本文关键字:时间 控制器 域控 何获得 | 更新日期: 2023-09-27 18:09:40

我正在使用这个代码,但我得到错误

system.directoryservices.activedirectory。Directorycontext不包含目标

的定义
using System.DirectoryServices.ActiveDirectory;
static void FindAnyTimeServer(DirectoryContext context)
{
    try
    {
        DomainController dc = DomainController.FindOne(context, LocatorOptions.TimeServerRequired);
        Console.WriteLine("A time server for {0} is {1}.", context.Target, dc.Name);
    }
    catch (ActiveDirectoryObjectNotFoundException)
    {
        Console.WriteLine("No time server was found in {0}.", context.Target);
    }
}

如何获得域控制器的时间

试试这个:

    DirectoryContext context = new
    DirectoryContext(DirectoryContextType.DirectoryServer, "insert_domain_controller_name_here");
    DomainController dc =
    DomainController.GetDomainController(context);
    DateTime dt = dc.CurrentTime;
    MessageBox.Show("Domain Time is " +dt.ToLongTimeString());

根据你方要求:

static void FindAnyTimeServer(DirectoryContext context)
    {
        try
        {
            DomainController dc = DomainController.GetDomainController(context);
            DateTime dt = dc.CurrentTime;
            Console.WriteLine("A time server for {0} is {1}.", context.Name, dt);           
        }
        catch (ActiveDirectoryObjectNotFoundException)
        {
            Console.WriteLine("No time server was found in {0}.", context.Name);
        }
    }