2012-10-29 28 views
0

我正在嘗試創建一個應用程序,允許我輸入用戶名並通過修改HKEY_USERS \ UserSID下的註冊表來切換該用戶的默認打印機。儘管我似乎無法寫入註冊表的那部分值。也許這是Windows的限制?這是我迄今爲止的代碼。Writing from HKEY_USERS

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) 
+2

您有權利使用?你確定你在做什麼是一個好主意嗎?我不是。 – Wug

+1

此外,用戶是否登錄?如果他們沒有登錄,那麼他們的註冊表配置單元將不會被加載。發生了什麼故障? – Luke

+0

您的應用程序是否針對x86平臺編譯並在x64操作系統上運行? – Steve

回答

0

我不認爲有一些Windows限制,因爲我多次寫入HKEY_USERS \ SID。但我爲此使用了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 
相關問題