C# 如果directoryEntry不存在

C# 如果directoryEntry不存在,c#,exists,directoryentry,C#,Exists,Directoryentry,在asp.net c#中使用目录项,如果调用: ADUtils newAdClass = new ADUtils("dl-dom", "ad.test", "Password?1"); List<string> domUsers = newAdClass.GetDomainUsers(); ---------------------------------------------------------------------------------- -----------

在asp.net c#中使用目录项,如果调用:

ADUtils newAdClass = new ADUtils("dl-dom", "ad.test", "Password?1");
    List<string> domUsers = newAdClass.GetDomainUsers();
----------------------------------------------------------------------------------
-------------------------------------------------------------------------------

public List<string> GetDomainUsers()
{
    //returned list
    List<string> domainUsers = new List<string>();

    //create connection
    DirectoryEntry entry = new DirectoryEntry(_lDAPPath, _ldapUser, _ldapPassword);
    DirectorySearcher search = new DirectorySearcher(entry);

    //search subtree nodes
    search.SearchScope = SearchScope.Subtree;

    //Active Directory LDAP: All email users (alternate)
    search.Filter = "(&(objectClass=user)(objectcategory=person))";

    //create results objects from search object 
    SearchResultCollection results = search.FindAll();

    //run through list, for each entry remove 'CN=' and add 'user' to list
    for (int i = 0; i < results.Count; i++)
    {
        DirectoryEntry de = results[i].GetDirectoryEntry();
        string user = de.Name.Replace("CN=", "");
        domainUsers.Add(user);
    }
    return domainUsers;
}
这在我的代码中抛出了一个错误,因此我试图使用
http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.exists%28v=vs.110%29.aspx
存在

但是DirectoryEntry返回一个对象,当我需要测试字符串时,我认为路径是错误的……有什么想法吗

string entry1 = _lDAPPath + "," + _ldapUser + "," + _ldapPassword;
//entry1 returns: LDAP://DC=dl-dom,ad.test,Password?1

if (DirectoryEntry.Exists(entry1))
{
    DirectorySearcher search = new DirectorySearcher(entry);
当我使用上面的代码时,我得到了一个异常

An invalid dn syntax has been specified.
建造商:

public ADUtils(string LDAPDomain, string ADUser, string ADUserPwd)
{
    _lDAPPath = "LDAP://DC=" + LDAPDomain;
    _ldapUser = ADUser;
    _ldapPassword = ADUserPwd;
}

例如,假设域是“example.com”
测试路径应该是
LDAP://example.com

如果不提供DN,它将自动连接到域根对象。因此,在上面的示例中,它实际获取的对象是
LDAP://example.com/DC=example,DC=com
在访问之前不要使用
Exists()
函数进行测试。LDAP目录是易变的,可以从您的下面更改。这是一个比赛条件

相反,请使用try/catch块,并在异常失败时处理异常:

try
{
    //create results objects from search object 
    SearchResultCollection results = search.FindAll();

    //run through list, for each entry remove 'CN=' and add 'user' to list
    for (int i = 0; i < results.Count; i++)
    {
        DirectoryEntry de = results[i].GetDirectoryEntry();
        string user = de.Name.Replace("CN=", "");
        domainUsers.Add(user);
    }
}
catch(Excpetion e)
{
    //add code here to process the error

    //after debugging, you may even decide to just swallow the exception 
    // and return an empty collection
}
试试看
{
//从搜索对象创建结果对象
SearchResultCollection results=search.FindAll();
//运行列表,对于每个条目,删除“CN=”并将“user”添加到列表中
for(int i=0;i
??我提供了在我的Q-im查询中字符串路径是错误的,但不确定我在哪里出错字符串myADSPath=“LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com”;到目前为止,我认为唯一正确的是LDAP://DC=DL-DOM,
try
{
    //create results objects from search object 
    SearchResultCollection results = search.FindAll();

    //run through list, for each entry remove 'CN=' and add 'user' to list
    for (int i = 0; i < results.Count; i++)
    {
        DirectoryEntry de = results[i].GetDirectoryEntry();
        string user = de.Name.Replace("CN=", "");
        domainUsers.Add(user);
    }
}
catch(Excpetion e)
{
    //add code here to process the error

    //after debugging, you may even decide to just swallow the exception 
    // and return an empty collection
}