2

我正在使用此代码,但我收到错误

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

1 回答 1

2

尝试这个 :

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

    }
于 2013-04-16T14:26:06.627 回答