共用方式為


叫用 ADSI 方法

如果 ADSI 介面支援 IDispatch 介面,則您可以使用 Invoke 方法來存取該介面的方法。這也可以套用到您過去已新增的任何 ADSI 擴充程式。您不需要包含 ADSI 程式庫即可使用 Invoke 方法。

當基礎方法失敗時,可能會發生 TargetInvocationException 例外狀況。InnerException 屬性 (屬於 TargetInvocationException 物件) 是一個 COMException 物件,其中包含所發生實際錯誤的相關資訊。

下列 C# 範例示範如何叫用 IADsUser 介面 SetPassword 方法來設定密碼。如需有關 IADsUser 介面或 SetPassword 方法的詳細資訊,請參閱 MSDN Library (網址為 https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsUser>或<IADsUser::SetPassword>。

DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("SetPassword", new object[] {SecurelyStoredPassword});

下列 C# 範例示範如何叫用 IADsUser 介面 ChangePassword 方法來變更密碼。如需有關 IADsUser 介面或 ChangePassword 方法的詳細資訊,請參閱 MSDN Library (網址為 https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsUser>或<IADsUser::ChangePassword>。

DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("ChangePassword", new object[] {SecurelyStoredPassword, NewSecurelyStoredPassword});

下列 C# 範例示範如何叫用 IADsGroup 介面 Members 方法來擷取群組的成員。如需有關 IADsGroup 介面或 Members 方法的詳細資訊,請參閱 MSDN Library (網址為 https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsGroup>或<IADsGroup::Members>。

DirectoryEntry grpEntry = new DirectoryEntry("LDAP://CN=Enterprise Admins,CN=Users,DC=Fabrikam, DC=com");
object members = grpEntry.Invoke("Members",null);
foreach( object member in (IEnumerable) members)
{
    DirectoryEntry x = new DirectoryEntry(member);
    Console.WriteLine(x.Name);
}

請參閱

參考

System.DirectoryServices
DirectoryEntry
TargetInvocationException
COMException
TargetInvocationException

概念

叫用 ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.