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
[C#]
public DirectoryEntry Add(
 string name,
 string schemaClassName
);
[C++]
public: DirectoryEntry* Add(
 String* name,
 String* schemaClassName
);
[JScript]
public function Add(
   name : String,
 schemaClassName : String
) : DirectoryEntry;

Parameters

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

Return Value

A DirectoryEntry that represents the new entry.

Exceptions

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

Remarks

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.

Example

[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'.
myDirectoryEntry.CommitChanges()
Console.WriteLine(myDirectoryEntry.Name + " entry is created in container.")

[C#] 
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'.
myDirectoryEntry.CommitChanges();
Console.WriteLine (myDirectoryEntry.Name + " entry is created in container.");

[C++] 
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'.
myDirectoryEntry->CommitChanges();
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.

Requirements

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

Show:
© 2016 Microsoft