DirectoryEntries.Find Method (String, String)

Returns the member of this collection with the specified name and of the specified type.

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)

public DirectoryEntry Find(
	string name,
	string schemaClassName
)

Parameters

name
Type: System.String

The name of the child directory object for which to search.

schemaClassName
Type: System.String

The class name of the child directory object for which to search.

Return Value

Type: System.DirectoryServices.DirectoryEntry
A DirectoryEntry object that represents the child object that was found.

ExceptionCondition
InvalidOperationException

The Active Directory Domain Services object is not a container.

COMException

An error occurred during the call to the underlying interface.

If no matching result is found, a DirectoryServicesCOMException with error code 0x2030 is thrown.

The following Visual Basic .NET example creates a new DirectoryEntry object with the specified path, then creates a new entry in the container and saves it. It attempts to retrieve the newly created entry.

Try
     Dim myEntry1 As DirectoryEntry
     Dim myEntry2 As DirectoryEntry
     Dim strPath As String = "LDAP://DC=fabrikam,DC=com"

     ' Create a 'DirectoryEntry' object with the given path.
     Dim myDE As New DirectoryEntry(strPath)
     Dim myEntries As DirectoryEntries = myDE.Children

     ' Create a new entry in the container.
     myEntry1 = myEntries.Add("CN=Sample Entry", myDE.SchemaClassName)
     ' Save changes in the 'Active Directory Domain Services' store.
     myEntry1.CommitChanges()

     ' Find a child in the 'DirectoryEntries' collection which has the 
     ' specified name and type.
     myEntry2 = myEntries.Find("CN=Sample Entry", myDE.SchemaClassName)
     Console.WriteLine(myEntry2.Name + " is found in container.")

     Catch e As Exception
          Console.WriteLine("The following exception was raised : {0}", e.Message.ToString())
     End Try

The following C# example shows how to create a new DirectoryEntry object with the specified path, then creates a new entry in the container and saves it. It attempts to retrieve the newly- created entry.

using System;
using System.DirectoryServices; 

class MyClass1
{
   static void Main()
   {
      try
      {        
         DirectoryEntry myEntry1;
         DirectoryEntry myEntry2;
         String strPath = "LDAP://DC=fabrikam,DC=com";

         // Create a 'DirectoryEntry' object with the given path.
         DirectoryEntry myDE = new DirectoryEntry(strPath);
         DirectoryEntries myEntries = myDE.Children;

         // Create a new entry in the container.
         myEntry1 = myEntries.Add("CN=Sample Entry",myDE.SchemaClassName);
         // Save changes in the 'Active Directory Domain Services' store.
         myEntry1.CommitChanges();

         // Find a child in the 'DirectoryEntries' collection which has the 
         // specified name and type.
         myEntry2 = myEntries.Find("CN=Sample Entry",myDE.SchemaClassName);
         Console.WriteLine (myEntry2.Name + " is found in container.");

      }
      catch(Exception e)
      {
         Console.WriteLine("The following exception was raised : {0}",e.Message);
      }
   }
}

The following C++ example creates a new DirectoryEntry object with the specified path, then creates a new entry in the container and saves it. It attempts to retrieve the new entry.

#using <mscorlib.dll>
#using <System.dll>
#using <System.Directoryservices.dll>

using namespace System;
using namespace System::DirectoryServices; 

int main() {
    try {        
        DirectoryEntry* myEntry1;
        DirectoryEntry* myEntry2;
        String* strPath = S"LDAP://DC=fabrikam,DC=com";

        // Create a 'DirectoryEntry' object with the given path.
        DirectoryEntry* myDE = new DirectoryEntry(strPath);
        DirectoryEntries* myEntries = myDE->Children;

        // Create a new entry in the container.
        myEntry1 = myEntries->Add(S"CN=Sample Entry", myDE->SchemaClassName);
        // Save changes in the 'Active Directory Domain Services' store.
        myEntry1->CommitChanges();

        // Find a child in the 'DirectoryEntries' collection which has the 
        // specified name and type.
        myEntry2 = myEntries->Find(S"CN=Sample Entry", myDE->SchemaClassName);
        Console::WriteLine ("{0} is found in container.", myEntry2->Name);
    } catch(Exception* e) {
        Console::WriteLine("The following exception was raised : {0}", e->Message);
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft