skuste tento priklad: Get local user accounts using VB.Net Reference System.DirectoryServices.AccountManagement Namespace System.DirectoryServices.AccountManagement They didn't have any examples for VB.Net. So, I examined the C# code that was contributed by someone else and translated it to return a list of user accounts on a local computer versus returning a list of computers. I submitted my first draft of the translated code to MSDN to post on their PrincipalSearcher Class page. Here, I'm expanding my example to demonstrate how to do basic error trapping and disposing of objects. Code for a VB.Net Windows Forms Application project : Form1.vb*...
Imports System
Imports System.DirectoryServices.AccountManagement
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ctx As PrincipalContext = New PrincipalContext(ContextType.Machine)
Dim up As UserPrincipal = New UserPrincipal(ctx)
Dim ps As PrincipalSearcher = New PrincipalSearcher(up)
Try
Dim results As PrincipalSearchResult(Of Principal) = ps.FindAll
For Each cr As Principal In results
ListBox1.Items.Add(cr.Name)
Next
Me.Show()
ps.Dispose()
up.Dispose()
ctx.Dispose()
Catch ex As Exception
MsgBox(ex.ToString)
End
End Try
End Sub
End Class
|