Public Class Filters
Inherits AdvancedFilters
Public Sub New(ByVal p As Principal)
MyBase.New(p)
End Sub
Public Sub CreatedAfter(ByVal value As Date)
Dim strValue = value.ToString("yyyyMMddHHmmss.0Z")
AdvancedFilterSet("WhenCreated", strValue, GetType(String), MatchType.GreaterThanOrEquals)
End Sub
End Class
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using context = New PrincipalContext(ContextType.Domain)
Using userPrin As New UserPrincipal(context) With {
.Enabled = True}
Dim filter = New Filters(userPrin)
filter.CreatedAfter(Date.UtcNow.AddDays(-20))
For Each result In (New PrincipalSearcher(userPrin)).FindAll()
Dim de As DirectoryEntry = TryCast(result.GetUnderlyingObject(), DirectoryEntry)
Dim dn1 As String = de.Properties("CN")(0)
Dim dn2 As String = de.Properties("whenCreated")(0)
DataGridView1.Rows.Add(dn1, dn2)
Next result
End Using
End Using
End Sub
代码显示从当前日期开始的最近20天内创建的用户。
如何显示从DateTimePicker +20天中选择的日期创建的用户列表
例如:
DateTimePicker = 09/08/2021
用户列表应包含创建日期为08/09/2021+20天(28/09/2021)的用户
找到解决方案了。如果有人告诉我如何做得更好,我将感激不尽。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DataGridView1.Rows.Clear()
Using context = New PrincipalContext(ContextType.Domain)
Using userPrin As New UserPrincipal(context) With {
.Enabled = True}
Dim filter = New Filters(userPrin)
Dim FirstDate As Date = DateTimePicker1.Value
Dim SecondDate As Date = DateTimePicker2.Value
Dim ts As TimeSpan = SecondDate.Subtract(FirstDate)
Dim differenceInDays As Integer = ts.Days
filter.CreatedAfter(SecondDate.AddDays(-differenceInDays - 1))
For Each result In (New PrincipalSearcher(userPrin)).FindAll()
Dim de As DirectoryEntry = TryCast(result.GetUnderlyingObject(), DirectoryEntry)
Dim dn1 As String = de.Properties("CN")(0)
Dim dn2 As String = de.Properties("whenCreated")(0)
DataGridView1.Rows.Add(dn1, dn2)
Next result
End Using
End Using
End Sub