从HKEY_USERS写作

时间:2012-10-29 13:39:53

标签: windows vb.net registry sid

我正在尝试创建一个应用程序,允许我输入用户名并通过修改HKEY_USERS \ UserSID下的注册表来切换该用户的默认打印机。我似乎无法在注册表的该部分写入值。也许这是Windows限制?这是我到目前为止的代码。

    Dim strComputer = "."
    Dim objWMIService As Object = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Dim theUsername As String = TextBox1.Text
    Dim theDomain As String = TextBox2.Text

    Dim objAccount As Object = objWMIService.Get("Win32_UserAccount.Name='" & theUsername & "',Domain='" & theDomain & "'")

    Dim theport As RegistryKey
    theport = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices")
    Dim val As Object = theport.GetValue(ListBox1.SelectedItem)
    theport.Close()
    Dim theSid As String = objAccount.sid
    Dim theKey As RegistryKey = Registry.Users.OpenSubKey(theSid + "\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows", True)

1 个答案:

答案 0 :(得分:0)

我不认为有一些Windows限制,因为我多次写入HKEY_USERS \ SID。但我为此目的使用了vbscript。另外我应该警告你,如果记录了它们,你只能读取和写入Users注册表。对于未登录的用户 - 使用ActiveSetup。

我的脚本在vbs上为所有登录用户写了一些注册表。希望你能适应VB.NET。

Option Explicit

Const HKEY_USERS = &H80000003
Dim objReg, objWMI, colSessions, objSession, colList, colUsers, objUser, Domain, UserName, objUserAccount, SID, WshShell

Set WshShell = CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 

Set colSessions = objWMI.ExecQuery("Select * from Win32_LogonSession Where LogonType = 2 Or LogonType = 10") 
If colSessions.Count <> 0 Then 
    For Each objSession in colSessions 
        Set colUsers = objWMI.ExecQuery("Associators of " & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
        For Each objUser in colUsers 
            Domain = objUser.Domain : UserName = objUser.Name
            Set objUserAccount = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2:Win32_UserAccount.Domain='" & Domain & "',Name='" & UserName & "'")
            SID = objUserAccount.SID
            objReg.CreateKey HKEY_USERS, SID & "\Control Panel\Desktop"
            objReg.SetStringValue HKEY_USERS, SID & "\Control Panel\Desktop", "Example", "1"
            objReg.SetDwordValue HKEY_USERS, SID & "\Control Panel\Desktop", "Example", "2"         

        Next 
    Next 
End If