如何在VB.NET中获取所有本地存储的用户配置文件的用户名和域?

时间:2013-03-11 22:42:06

标签: vb.net user-profile

如何使用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

2 个答案:

答案 0 :(得分: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的导入

答案 1 :(得分:1)

我认为代码项目中的这篇文章http://www.codeproject.com/Articles/19689/Working-with-Active-Directory-in-VB-NET将对您有所帮助。当你通读它时,你应该看到答案'ansgar-wiechers'和本文之间的相似之处。