Add a key to HKEY_CURRENT_USER for all users

孤街浪徒 提交于 2020-08-22 08:39:45

问题


I have an installer which install a key on the HKEY_CURRENT_USER. When I run the installer, it only add it on the user that is installing. Is there a way to add the key to all user at once ?

Thanks


回答1:


You'd have to go through all the different users under HKEY_USERS, which requires elevated rights. And doesn't capture any users that have not yet been created. That's just the wrong approach.

The way to do it is to add the default values to a corresponding key under HKLM at install time. When your program attempts to read from the registry, it looks in HKCU first, and if your key is not present, it copies the information from the corresponding key in HKLM to the key in HKCU.

A general rule of installer programs is that they should not rely on being run by the user that will subsequently use the program that has been installed. Certainly in corporate settings programs are usually installed under a user account that will never subsequently run the program being installed.




回答2:


In some cases, Active Setup may be the solution.

It works by adding a key to HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name% with a version number. When a user logs in Windows checks this location and compares it to HKCU\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%. If it is missing or a lower version then it runs whatever has been set in HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%\StubPath.

You can do some custom things this way, for example I used it to add a certain script (to map a network drive) to the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run the following way:

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive" /v "Version" /d "1" /t REG_SZ /f

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive" /v "StubPath" /d "reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v "MountDrive" /d "C:\map.cmd" /t REG_DWORD /f" /f

What happens here:

  • when the user next logs on, Active Setup checks if there is a string Version with value of 1 or greater in the registry under key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive. There is none, therefore it creates it, and also runs the second reg add command, which adds a string with a value C:\map.cmd under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
  • it only happens once, because on each consecutive log on Active Setup will find out that the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive now has a Version under it.

Sounds complicated, but makes sense once you figure it out, and very useful.

http://wpkg.org/Adding_Registry_Settings#Adding_entries_to_HKCU_for_all_users




回答3:


This is my process as I currently work in Windows 7

  • Find the key/s you want under HKCU in regedit, export it/them.

  • Now right click on the HKEY_USERS key and select load hive. Browse to C:\Users\Default and select NTUSER.DAT. Name the hive whatever you like (eg NAMEHERE).

  • in your exported reg file replace all instances of [HKEY_CURRENT_USER\ with [HKEY_USERS\NAMEHERE\ (or whatever you named your hive) and save the file

  • double click the reg file to merge it

  • Highlight the NAMEHERE key and select unload hive.

Now for any new profiles they get those keys. Existing profiles don't though so you won't see it work if you log in with a pre-existing profile. If you want to delete the profile I find the cleanest way is right click computer, select properties, advanced system settings and then select settings under user profiles. Highlight the profile you want to delete and select delete.

It seems this doesn't work all the time. I'm here looking for information about why this isn't working for the keys I'm trying to add under HKEY_CURRENT_USER\Software\Classes\ but until now this process has always worked. I'll add a note if I see why that isn't working - I'm currently wondering if I have to add it through policy in the user context.




回答4:


I think the best way to do it, is by GPO.

Create a GPO that will modify the HKCU that apply the change you want to make to the users affected by that GPO.

The following Microsoft KB might help to implement this:

http://technet.microsoft.com/en-us/library/bb742499.aspx




回答5:


No, there's no way to do it.

Think of it in this scenario: you installed the app, then a new user account is created. How can you add a registry key to this new account during installation?


Or the only way to do it is when that user starts your application. That is your application creates the key with default values at the first launch.



来源:https://stackoverflow.com/questions/16221250/add-a-key-to-hkey-current-user-for-all-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!