Imports LzmTW.DirectoryServices ''' <summary> ''' PathService結合ldapPath專爲簡化AD域組織單元、用戶、計算機、網絡打印機的節點管理而設計。 ''' PathService將PathService與DirectoryEntry、Princial緊密結合在一起。 ''' 此爲示例。示例後貼出主要代碼,全部代碼計劃以資源下載提供。 ''' ''' 僅供參考。水如煙(http://www.cnblogs.com/LzmTW,http://blog.csdn.net/lzmtw) ''' 20090310 ''' </summary> Public Class PathServiceDemo Public Sub Test() Dim service As PathService service = (New Logon).CreateDomainService.CreatePathService service.Path = "CN=水如煙,OU=VB,OU=NET,OU=CSDN用戶" output(service) '對象路徑:LDAP://myServer.myDomain.local/CN=水如煙,OU=VB,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:True service.Name = "lzmtw" output(service) '對象路徑:LDAP://myServer.myDomain.local/CN=lzmtw,OU=VB,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:False service.Add() output(service) '對象路徑:LDAP://myServer.myDomain.local/CN=lzmtw,OU=VB,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:True Dim parent As PathService = service.GetParentService output(parent) '對象路徑:LDAP://myServer.myDomain.local/OU=VB,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:True parent.Name = "CSharp" output(parent) '對象路徑:LDAP://myServer.myDomain.local/OU=CSharp,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:True service.MoveTo(parent.Path) output(service) '對象路徑:LDAP://myServer.myDomain.local/CN=lzmtw,OU=CSharp,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:True service.Delete() output(service) '對象路徑:LDAP://myServer.myDomain.local/CN=lzmtw,OU=CSharp,OU=NET,OU=CSDN用戶,DC=myDomain,DC=local 存在否:False End Sub Private Sub output(ByVal service As PathService) Console.WriteLine("對象路徑:{0} 存在否:{1}", service.Path, service.IsExists) End Sub End Class
主要代碼:
Public Class PathService Inherits ldapPath Private gEntry As DirectoryEntry = Nothing Private gPrincipal As Principal = Nothing Private gIsExists As Boolean Private gDomainService As DomainService Private gSchemaClassName As String Private gobjectCategory As String Private gWhenCreated As Nullable(Of DateTime) Friend Sub New(ByVal service As DomainService) MyBase.New(service.Logon.ConnectedServerName) Me.gDomainService = service Me.OnPathChanged() End Sub Public ReadOnly Property DomainService() As DomainService Get Return gDomainService End Get End Property Protected ReadOnly Property Entry() As DirectoryEntry Get Return gEntry End Get End Property Protected ReadOnly Property Principal() As Principal Get Return gPrincipal End Get End Property ''' <summary> ''' 獲取一個值,表示節點對象是否存在 ''' </summary> Public ReadOnly Property IsExists() As Boolean Get Return gIsExists End Get End Property ''' <summary> ''' 獲取或設置節點對象的架構類的名稱 ''' </summary> ''' <remarks >僅當節點不存在時才能夠設置架構類名稱(添加節點時用到)</remarks> Public Property SchemaClassName() As String Get Return gSchemaClassName End Get Set(ByVal value As String) If Me.IsExists Then Return If Me.DomainService.FindActiveDirectorySchemaClass(value) Is Nothing Then Throw New Exception("域中無此架構類。") gSchemaClassName = value End Set End Property ''' <summary> ''' 獲取節點對象的架構類 ''' </summary> Public ReadOnly Property SchemaClass() As ActiveDirectorySchemaClass Get Return Me.DomainService.FindActiveDirectorySchemaClass(Me.SchemaClassName) End Get End Property ''' <summary> ''' 獲取節點對象類別 ''' </summary> Public ReadOnly Property ObjectCategory() As String Get Return gobjectCategory End Get End Property ''' <summary> ''' 獲取節點對象創建時間 ''' </summary> Public ReadOnly Property WhenCtreated() As Nullable(Of Date) Get Return gWhenCreated End Get End Property ''' <summary> ''' 獲取節點對象屬性 ''' </summary> ''' <param name="propertyName">屬性名稱</param> Public ReadOnly Property PropertyObject(ByVal propertyName As String) As PropertyValueCollection Get Return Me.Entry.Properties(propertyName) End Get End Property ''' <summary> ''' 獲取節點對象屬性值 ''' </summary> ''' <param name="propertyName">屬性名稱</param> Public ReadOnly Property PropertyValue(ByVal propertyName As String) As Object Get Return Me.PropertyObject(propertyName).Value End Get End Property ''' <summary> ''' 獲取與當前節點對象相符的DirectoryEntry實例 ''' </summary> Public Function GetCurrentDirectoryEntry() As DirectoryEntry Return Me.Entry End Function ''' <summary> ''' 獲取與當前節點對象相符的Principal實例 ''' </summary> Public Function GetCurrentPrincipal() As Principal Return Me.Principal End Function ''' <summary> ''' 從域中重新檢索當前節點對象 ''' </summary> Public Sub Reload() Me.Path = Me.Path End Sub ''' <summary> ''' 添加節點至節點父級對象成爲一個新對象。由SchemaClassName指定新對象的架構類名。 ''' </summary> ''' <param name="keyValues">屬性值集合。如果需要,生成對象後更新對象屬性</param> Public Sub Add(Optional ByVal keyValues As List(Of KeyValuePair(Of String, Object)) = Nothing) If Me.IsExists Then Throw New Exception("節點對象已存在") Dim parentPathService As PathService = Me.GetParentService If Not parentPathService.IsExists Then parentPathService.Dispose() Throw Me.ObjectNotExistsException(True) End If Dim tmpEntry As DirectoryEntry = parentPathService.Entry.Children.Add(Me.NameForLdap, Me.SchemaClassName) If keyValues IsNot Nothing Then For Each pair As KeyValuePair(Of String, Object) In keyValues tmpEntry.Properties(pair.Key).Value = pair.Value Next End If tmpEntry.CommitChanges() Me.Path = tmpEntry.Path tmpEntry.Dispose() tmpEntry = Nothing parentPathService.Dispose() End Sub ''' <summary> ''' 刪除當前節點對象 ''' </summary> Public Sub Delete() If Not Me.IsExists Then Throw ObjectNotExistsException() Me.Entry.Parent.Children.Remove(Me.Entry) Me.Reload() End Sub ''' <summary> ''' 移動當前節點對象至與指定路徑標識相符的父級 ''' </summary> ''' <param name="parentDistinguishedName">父級路徑標識</param> ''' <remarks></remarks> Public Sub MoveTo(ByVal parentDistinguishedName As String) If Not Me.IsExists Then Throw ObjectNotExistsException() Dim parentService As PathService = Me.CreateNewInstance parentService.Path = parentDistinguishedName If Not parentService.IsExists Then parentService.Dispose() Throw ObjectNotExistsException() End If Me.Entry.MoveTo(parentService.Entry) Me.Path = Me.Entry.Path parentService.Dispose() End Sub ''' <summary> ''' 更改當前節點對象的名稱 ''' </summary> ''' <param name="newName">新名稱</param> Public Sub Rename(ByVal newName As String) If Not Me.IsExists Then Throw ObjectNotExistsException() Dim tmp As ldapPath = Me.Clone tmp.Name = newName Me.Entry.Rename(tmp.NameForLdap) Me.Path = Me.Entry.Path tmp.Dispose() End Sub ''' <summary> ''' 更新當前節點對象屬性 ''' </summary> Public Sub Update() If Not Me.IsExists Then Throw ObjectNotExistsException() Me.Entry.CommitChanges() Me.Reload() End Sub ''' <summary> ''' 創建PathService新實例 ''' </summary> ''' <remarks >指向根節點</remarks> Public Function CreateNewInstance() As PathService Return New PathService(Me.DomainService) End Function ''' <summary> ''' 獲取當前節點對象的父級PathService實例 ''' </summary> Public Function GetParentService() As PathService Dim instance As PathService = Me.CreateNewInstance instance.Path = Me.ParentDistinguishedName Return instance End Function Private Function ObjectNotExistsException(Optional ByVal IsParent As Boolean = False) As Exception If IsParent Then Return New Exception("父節點對象不存在。") End If Return New Exception("節點對象不存在。") End Function Protected Overrides Sub OnPathChanged() gEntry = Me.DomainService.CreateDirectoryEntry(Me.Path) Try gSchemaClassName = Me.Entry.SchemaClassName gPrincipal = Me.DomainService.DirectoryEntryToPrincipal(Me.Entry) gWhenCreated = CType(Me.PropertyValue("whenCreated"), Date?) gobjectCategory = Me.PropertyValue("objectCategory").ToString gIsExists = True Catch ex As Exception gIsExists = False gobjectCategory = Nothing gWhenCreated = New Nullable(Of DateTime) If Principal IsNot Nothing Then gPrincipal.Dispose() gPrincipal = Nothing End If End Try MyBase.OnPathChanged() End Sub '對DomainService不作處理。 Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Me.IsExists Then Me.gPrincipal.Dispose() Me.gPrincipal = Nothing Me.gEntry.Dispose() Me.gEntry = Nothing End If End If MyBase.Dispose(disposing) End Sub End Class