Add Method
Collapse the table of content
Expand the table of content

DirectoryEntries.Add Method

Creates a request to create a new entry in the container.

[Visual Basic]
Public Function Add( _
   ByVal name As String, _
   ByVal schemaClassName As String _
) As DirectoryEntry
public DirectoryEntry Add(
 string name,
 string schemaClassName
public: DirectoryEntry* Add(
 String* name,
 String* schemaClassName
public function Add(
   name : String,
 schemaClassName : String
) : DirectoryEntry;


The name of the new entry.
The name of the schema used for this new entry.

Return Value

A DirectoryEntry that represents the new entry.


Exception Type Condition
COMException An error occurred during the call to the underlying interface.


Note   You must call the CommitChanges method on the new entry to make the creation permanent. This allows you to 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.


[Visual Basic, C#, C++] The following example creates a new DirectoryEntry with a specified path, then creates a new entry in the container and saves it.

[Visual Basic] 
Dim strPath As String = "IIS://localhost/W3SVC/1/Root"
' Create a new 'DirectoryEntry' 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("Sample", myDE.SchemaClassName)
' Save changes of entry in the 'Active Directory'.
Console.WriteLine(myDirectoryEntry.Name + " entry is created in container.")

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

// Create a new 'DirectoryEntry' 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("Sample", myDE.SchemaClassName);
// Save changes of entry in the 'Active Directory'.
Console.WriteLine (myDirectoryEntry.Name + " entry is created in container.");

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

// Create a new 'DirectoryEntry' 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"Sample", myDE->SchemaClassName);
// Save changes of entry in the 'Active Directory'.
Console::WriteLine("{0} entry is created in container.", myDirectoryEntry->Name);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

DirectoryEntries Class | DirectoryEntries Members | System.DirectoryServices Namespace | DirectoryEntry

© 2016 Microsoft