0

我正在尝试创建一个应用程序,该应用程序允许我输入用户名并通过修改 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)
4

1 回答 1

0

我不认为有一些 Windows 限制,因为我多次写信给 HKEY_USERS\SIDs。但我为此使用了 vbscript。此外,我应该警告您,如果用户已登录,您只能读取和写入用户注册表。对于未登录的用户 - 使用 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 
于 2012-10-30T13:36:05.800 回答