Export (0) Print
Expand All

DirectoryEntries.Add Method

Creates a new entry in the container.

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

public DirectoryEntry Add(
	string name,
	string schemaClassName
)

Parameters

name
Type: System.String

The name of the new entry.

schemaClassName
Type: System.String

The name of the schema that is used for the new entry.

Return Value

Type: System.DirectoryServices.DirectoryEntry
A DirectoryEntry object that represents the new entry.

ExceptionCondition
COMException

An error occurred during the call to the underlying interface.

You must call the CommitChanges method on the new entry to make the creation permanent. When you call this method, you can then set mandatory property values on the new entry. The providers each have different requirements for properties that need to be set before a call to the CommitChanges method is made. If those requirements are not met, the provider might throw an exception. Check with your provider to determine which properties must be set before committing changes.

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

Dim strPath As String = "IIS://localhost/W3SVC/1/Root"
' Create a new 'DirectoryEntry' object with the given path.
Dim myDE As New DirectoryEntry(strPath)
Dim myEntries As DirectoryEntries = myDE.Children
' Create a new entry 'Sample' in the container.
Dim myDirectoryEntry As DirectoryEntry = myEntries.Add("CN=Sample", myDE.SchemaClassName)
' Save changes of entry in the 'Active Directory Domain Services'.
myDirectoryEntry.CommitChanges()
Console.WriteLine(myDirectoryEntry.Name + " entry is created in container.")

The following C# example creates a new DirectoryEntry object with a specified path, then creates a new entry in the container and saves it.

[C#]

String strPath = "IIS://localhost/W3SVC/1/Root";

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

// Create a new entry 'Sample' in the container.
DirectoryEntry myDirectoryEntry = 
   myEntries.Add("CN=Sample", myDE.SchemaClassName);
// Save changes of entry in the 'Active Directory Domain Services'.
myDirectoryEntry.CommitChanges();
Console.WriteLine (myDirectoryEntry.Name + " entry is created in container.");

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

String* strPath = S"IIS://localhost/W3SVC/1/Root";

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

// Create a new entry 'Sample' in the container.
DirectoryEntry* myDirectoryEntry = myEntries->Add(S"CN=Sample", myDE->SchemaClassName);
// Save changes of entry in the 'Active Directory Domain Services'.
myDirectoryEntry->CommitChanges();
Console::WriteLine("{0} entry is created in container.", myDirectoryEntry->Name);

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft