用 DirectorySearcher 执行查询

mfood 2003-05-12 11:16:25
用 DirectorySearcher 执行查询
DirectoryEntry root = new DirectoryEntry("LDAP://redmond/CN=Users,DC=redmond,DC=corp,DC=microsoft,DC=com");

DirectorySearcher searcher=new DirectorySearcher(root);
searcher.Filter =“(&(objectClass=user)(lastName= Davis))”;
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("title");
searcher.PropertiesToLoad.Add("department");
SearchResultCollection results = searcher.FindAll();

foreach(SearchResult result in results)
{
string cn=result.Properties["cn"][0].tostring();
string title=result.Properties["title"][0].tostring();
string department=result.Properties["department"][0].tostring();


}
但是当result.Properties["x"][0].tostring()为空时。foreach语句自动结束一
轮循环。不会接着执行下一句.并会出错了.
提示:未实例化的.....
...全文
215 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lx1920 2003-05-13
  • 打赏
  • 举报
回复
result.Properties["cn"][0]是object,如果为空,当然会报错。
你应该先判断result.Properties["cn"][0]是否为null

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧