Share via


設定目錄物件的屬性

本主題說明並提供使用單一值設定目錄物件屬性的程式碼範例。

使用下列方法來修改屬性值:

在設定屬性值時,資料會存至屬性快取中。若要將新資料寫入目錄中,請呼叫 CommitChanges 方法。如需詳細資訊,請參閱屬性快取

下列程式碼範例示範如何使用 Value 屬性。

Try
    Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com")
    ent.Properties("sn").Value = "Barr"
    ent.CommitChanges()
Catch COMEx As COMException
    ' If a COMException is thrown, then the following code example can catch the text of the error.
    ' For more information about handling COM exceptions, see Handling Errors.
    Console.WriteLine(COMEx.ErrorCode)
End Try
try
{
    DirectoryEntry ent = new DirectoryEntry(
        "LDAP://Fabrikam/CN=My Username,CN=Users,DC=Fabrikam,DC=com");
    ent.Properties["sn"].Value = "Barr";
    ent.CommitChanges();
}
catch (COMException COMEx)
{
    // If a COMException is thrown, then the following code example can catch the text of the error.
    // For more information about handling COM exceptions, see Handling Errors.
    Console.WriteLine(COMEx.ErrorCode);
}

請參閱

參考

System.DirectoryServices
PropertyValueCollection
DirectoryEntry

概念

目錄物件屬性
屬性快取
具有多個值的屬性

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.