我正在使用下面的代码来访问我的AD中的用户列表,但是在将用户添加到我的组合框的那一行,我遇到了一个空引用异常。
PrincipalContext AD = new PrincipalContext(ContextType.Domain, "mydomainip");
UserPrincipal u = new UserPrincipal(AD);
PrincipalSearcher search = new PrincipalSearcher(u);
foreach (UserPrincipal result in search.FindAll())
{
if (result.DisplayName != null)
{
comboBox2.Items.Add(result.DisplayName);
}
}
我错在哪里了呢?
我用 Console.WriteLine(result.DisplayName)
替换了下拉框,现在它可以正常工作。
comboBox2
是否已经被创建?您可以在调试时检查comboBox2
或comboBox2.Items
是否为 NULL 吗? - marc_s