4
我正在使用以下代码来访问我的AD中的用户列表,但是在将用户添加到我的组合框的行上,我得到一个空引用异常。从Active Directory PrincipalContext获取所有用户
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)
替换了组合框,它工作正常。
我试过了。它不起作用。 – user541597
@ user541597:嗯 - 即使在运行此代码时创建了'comboBox2'?你可以在调试时检查'comboBox2'或'comboBox2.Items'是否为NULL? –
在初始化组件之前调用该方法。 – user541597