我现在想要在一台机器上检索这台机器所在域中的一个User,但是使用如下方法在大多数的情况下是好使的:
string LDAPstring = "GC:";
DirectoryEntry de = new DirectoryEntry(LDAPstring);
IEnumerator enumerator = de.Children.GetEnumerator();enumerator.MoveNext();DirectoryEntry current = (DirectoryEntry)enumerator.Current;DirectorySearcher search = new DirectorySearcher(current); 
...(实际检索过程略)可是最近却在一台域控中出现了这样的错误:“Enumerator is positioned before the first item or after the last item.”
我知道这句话是在MoveNext之后运行“DirectoryEntry current = (DirectoryEntry)enumerator.Current;”时抛出来的,但是不知道具体原因。
希望有高手能帮帮忙不论是针对这个错误有解决方案,还是给出另一种取得本地域的解决方案都会给分的。
取本地域的时候要考虑到这台机器不是域控制器的情况,谢谢。

解决方案 »

  1.   

    private TreeNode AddNode(TreeNode parant,string text)
    {
             TreeNode node = new TreeNode(text);
             parant.Nodes.Add(node);
             return node;
    }
    private void EnumChildren(DirectoryEntry entry,TreeNode entryNode)
    {
           if(entry.Children!=null)        
             {
                     foreach(DirectoryEntry i in entry.Children)                 {
                              EnumChildren(i,AddNode(entryNode,i.Name));
                     }
             }
    }
    private void Enumerate(string path)
    {
             try 
             {
                     using(DirectoryEntry root = new DirectoryEntry(path))
                     {
                              TreeNode node = new TreeNode(root.Name);
                              treeView1.Nodes.Add(node);
                              EnumChildren(root,node);
                     }
             }
             catch {}
    }
      

  2.   

    谢谢你的回答!
    你这里的Enumerate(string path)函数的参数应该是LDAP路径吧?
    我是想得到本机所在的域,如果我知道这个LDAP路径的话我就可以直接连接了,但是现在我并不知道呀。
    我自己使用了另一种方法来尝试获得本机所在的域,就是使用Domain类里的GetCurrentDomain的方法,然后去获得这个Domain的DC的IP地址(前提是这台机器不是08的环境,否则可能会得到IPV6的地址)。最后组成LDAP路径然后重新构造DirectorySearcher对象进行检索。
    但是我总认为还会有其他更好的办法,或者是对我所说的那个异常信息有更好的解释。