Renaming an Object

DirectoryEntry provides the Rename method for renaming objects in the directory.

The following code example shows how to use Rename to change the name of a User object.

[Visual Basic .NET]

Try
    ' Bind to the user object to modify.
    Dim child As New DirectoryEntry("LDAP://CN=My User Name,OU=Marketing,DC=fabrikam,DC=com")
    ' Rename the object to Jeff Smith.
    child.Rename("CN=New User Name")
Catch Exception1 As Exception
    If (True) Then
        ' If a COMException is thrown, then the following code can capture the text of the error.
        ' For instructions about handling COM exceptions, see Handling Errors.
        Dim COMEx As System.Runtime.InteropServices.COMException = 
                CType(Exception1, System.Runtime.InteropServices.COMException)
        Console.WriteLine(COMEx.ErrorCode)
    End If 'break;
End Try '}

[C#]

try
{
    // Bind to the user object to modify.
    DirectoryEntry child = new DirectoryEntry("LDAP://CN=My User Name,OU=Marketing,DC=fabrikam,DC=com");
    // Rename the object to Jeff Smith.
    child.Rename("CN=New User Name");
}
catch (Exception Exception1)
{
    // If a COMException is thrown, then the following code can capture the text of the error.
    // For more information about handling COM exceptions, see Handling Errors.
    System.Runtime.InteropServices.COMException COMEx = 
    (System.Runtime.InteropServices.COMException) Exception1;
    Console.WriteLine(COMEx.ErrorCode);
)