我想写一个应用程序写入本地计算机上的所有用户一个指定的键(例如:我想设置所有用户的IEcollections夹的位置到相同的文件夹)
PS有人使用这些function? LoadUserProfile RegOpenCurrentUser CreateProcessAsUser
我做了很多次 这个想法是更新当前登录用户的HKCU(这很容易)。 然后,你必须枚举系统上的每个配置文件,并找到他们的ntuser.dat文件(这也很简单)。
找到ntuser.dat文件后,将其加载到HKLM配置单元中的临时密钥(我通常使用'HKLM \ TempHive'),然后编辑。
如果有超过1个用户登录,他们的配置文件将被加载在HKEY_USERS下,通过他们的SID。 只需更新该位置。
要修改任何新用户的设置,只需修改HKEY_USERS.DEFAULT下的相应密钥,或者使用下面的Delphi代码,通过加载默认用户的HKCU注册表配置单元(存储在ntuser.dat中)执行此操作。
更新 :我发现我的Delphi代码演示了如何更新未登录到系统的用户的HKCU配置单元。
这需要罗素Libby的“特权”组件, 可以在这里找到 。
//NOTE: sPathToUserHive is the full path to the users "ntuser.dat" file. // procedure LoadUserHive(sPathToUserHive: string); var MyReg: TRegistry; UserPriv: TUserPrivileges; begin UserPriv := TUserPrivileges.Create; try with UserPriv do begin if HoldsPrivilege(SE_BACKUP_NAME) and HoldsPrivilege(SE_RESTORE_NAME) then begin PrivilegeByName(SE_BACKUP_NAME).Enabled := True; PrivilegeByName(SE_RESTORE_NAME).Enabled := True; MyReg := TRegistry.Create; try MyReg.RootKey := HKEY_LOCAL_MACHINE; MyReg.UnLoadKey('TEMP_HIVE'); //unload hive to ensure one is not already loaded if MyReg.LoadKey('TEMP_HIVE', sPathToUserHive) then begin //ShowMessage( 'Loaded' ); MyReg.OpenKey('TEMP_HIVE', False); if MyReg.OpenKey('TEMP_HIVE\Environment', True) then begin // --- Make changes *here* --- // MyReg.WriteString('KEY_TO_WRITE', 'VALUE_TO_WRITE'); // // end; //Alright, close it up MyReg.CloseKey; MyReg.UnLoadKey('TEMP_HIVE'); //let's unload the hive since we are done with it end else begin WriteLn('Error Loading: ' + sPathToUserHive); end; finally FreeAndNil(MyReg); end; end; WriteLn('Required privilege not held'); end; finally FreeAndNil(UserPriv); end; end;
我也在前一段时间写了一个VBScript来完成这个任务。 我用它来修改一些Internet Explorer设置,但是您可以根据您的需要进行自定义。 它也演示了一般过程:
Option Explicit Dim fso Dim WshShell Dim objShell Dim RegRoot Dim strRegPathParent01 Dim strRegPathParent02 Set fso = CreateObject("Scripting.FileSystemObject") Set WshShell = CreateObject("WScript.shell") '============================================== ' Change variables here '============================================== ' 'This is where our HKCU is temporarily loaded, and where we need to write to it RegRoot = "HKLM\TEMPHIVE" ' strRegPathParent01 = "Software\Microsoft\Windows\CurrentVersion\Internet Settings" strRegPathParent02 = "Software\Microsoft\Internet Explorer\Main" ' '====================================================================== Call ChangeRegKeys() 'Sets registry keys per user Sub ChangeRegKeys 'Option Explicit On Error Resume Next Const USERPROFILE = 40 Const APPDATA = 26 Dim iResult Dim iResult1 Dim iResult2 Dim objShell Dim strUserProfile Dim objUserProfile Dim strAppDataFolder Dim strAppData Dim objDocsAndSettings Dim objUser Set objShell = CreateObject("Shell.Application") Dim sCurrentUser sCurrentUser = WshShell.ExpandEnvironmentStrings("%USERNAME%") strUserProfile = objShell.Namespace(USERPROFILE).self.path Set objUserProfile = fso.GetFolder(strUserProfile) Set objDocsAndSettings = fso.GetFolder(objUserProfile.ParentFolder) 'Update settings for the user running the script '(0 = default, 1 = disable password cache) WshShell.RegWrite "HKCU\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD" WshShell.RegWrite "HKCU\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ" strAppDataFolder = objShell.Namespace(APPDATA).self.path strAppData = fso.GetFolder(strAppDataFolder).Name ' Enumerate subfolders of documents and settings folder For Each objUser In objDocsAndSettings.SubFolders ' Check if application data folder exists in user subfolder If fso.FolderExists(objUser.Path & "\" & strAppData) Then 'WScript.Echo "AppData found for user " & objUser.Name If ((objUser.Name <> "All Users") and _ (objUser.Name <> sCurrentUser) and _ (objUser.Name <> "LocalService") and _ (objUser.Name <> "NetworkService")) then 'Load user's HKCU into temp area under HKLM iResult1 = WshShell.Run("reg.exe load " & RegRoot & " " & chr(34) & objDocsAndSettings & "\" & objUser.Name & "\NTUSER.DAT" & chr(34), 0, True) If iResult1 <> 0 Then WScript.Echo("*** An error occurred while loading HKCU: " & objUser.Name) Else WScript.Echo("HKCU loaded: " & objUser.Name) End If WshShell.RegWrite RegRoot & "\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD" WshShell.RegWrite RegRoot & "\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ" iResult2 = WshShell.Run("reg.exe unload " & RegRoot,0, True) 'Unload HKCU from HKLM If iResult2 <> 0 Then WScript.Echo("*** An error occurred while unloading HKCU: " & objUser.Name & vbcrlf) Else WScript.Echo(" unloaded: " & objUser.Name & vbcrlf) End If End If Else 'WScript.Echo "No AppData found for user " & objUser.Name End If Next End Sub
我们在前一天有完全相同的问题。
我们发现您可以打开HKEY_USERS
配置单元,并将更改写入每个用户的SID。
而且,如果您希望任何新用户的设置都存在,您还应该将这些设置应用于HKEY_USERS/.DEFAULT
项。
也就是说,只需将您的设置写入…
HKEY_USERS\S-1-5-XX-XXXXXXXX-XXXXXXXXX-XXXXXXXX-XXXX\Software\...
对于每个存在的SID和:
HKEY_USERS\.DEFAULT\Software\...