How to get a list of all domains?

后端 未结 2 1793
清酒与你
清酒与你 2021-01-01 03:32

I\'m trying to get all domains that are available in the Windows Login dialog (in the Domain dropdown).

I\'ve tried the following code but it only returns the domai

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 04:06

    Add a reference to System.DirectoryServices.dll

    using (var forest = Forest.GetCurrentForest())
    {
        foreach (Domain domain in forest.Domains)
        {
            Debug.WriteLine(domain.Name);
            domain.Dispose();
        }
    }
    

提交回复
热议问题