-4

如何使用 VB.NET 检索存储在计算机上的所有用户配置文件的用户名和域?下面的代码正是我需要的,但在 VBScript 中。我怎样才能在 VB.NET 中做同样的事情?

    Const HKLM = &H80000002
    Const profiles = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"

    sh = CreateObject("WScript.Shell")
    fso = CreateObject("Scripting.FileSystemObject")
    wmi = GetObject("winmgmts://./root/cimv2")
    reg = GetObject("winmgmts://./root/default:StdRegProv")

    reg.EnumKey(HKLM, profiles, subkeys)
    For Each sid In subkeys
        reg.GetStringValue(HKLM, profiles & "\" & sid, "ProfileImagePath", path)
        path = sh.ExpandEnvironmentStrings(path)
        If fso.FolderExists(path) Then
            acct = wmi.Get("Win32_SID.SID='" & sid & "'")
            CheckedListBox1.Items.Add(acct.ReferencedDomainName & "\" & acct.AccountName)
        End If
    Next
4

2 回答 2

2

对 Win32_UserAccount 的简单查询将提供所需的信息

Sub Main
    Dim oquery =  new System.Management.ObjectQuery("SELECT * FROM Win32_UserAccount")
    Dim mosearcher = new System.Management.ManagementObjectSearcher(oquery)
    Dim moc = mosearcher.Get()
    for each mo in moc
        Console.WriteLine(mo.Properties("Caption").Value.ToString())
        Console.WriteLine(mo.Properties("Domain").Value.ToString())
    Next
End Sub

需要引用 System.Management.dll 和导入 System.Management

于 2013-03-11T22:59:45.503 回答
1

我认为这篇关于 Code Project 的文章http://www.codeproject.com/Articles/19689/Working-with-Active-Directory-in-VB-NET会对您有所帮助。当您阅读它时,您应该会看到提供的答案“ansgar-wiechers”与本文之间的相似之处。

于 2013-03-11T22:49:42.717 回答