.net 为所有用户添加一个键到 HKEY_CURRENT_USER

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16221250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:13:51  来源:igfitidea点击:

Add a key to HKEY_CURRENT_USER for all users

.netinstallerregistry

提问by David Brunelle

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 ?

我有一个安装程序,它在 HKEY_CURRENT_USER 上安装了一个密钥。当我运行安装程序时,它只会将它添加到正在安装的用户上。有没有办法一次将密钥添加到所有用户?

Thanks

谢谢

回答by David Heffernan

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.

您必须查看 下的所有不同用户HKEY_USERS,这需要提升权限。并且不会捕获任何尚未创建的用户。那只是错误的方法。

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

这样做的方法是HKLM在安装时将默认值添加到相应的键下。当您的程序尝试从注册表中读取时,它HKCU首先查找,如果您的密钥不存在,它会将信息从相应的密钥 in 复制HKLMHKCU.

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.

安装程序的一般规则是,它们不应依赖于随后将使用已安装程序的用户运行。当然,在公司设置中,程序通常安装在一个用户帐户下,该帐户随后将永远不会运行正在安装的程序。

回答by Evgeny

In some cases, Active Setupmay be the solution.

在某些情况下,Active Setup可能是解决方案。

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.

它的工作原理是向HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%添加一个带有版本号的密钥。当用户登录 Windows 时,检查此位置并将其与HKCU\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name% 进行比较。如果它丢失或版本较低,则它运行在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\Runthe following way:

您可以通过这种方式进行一些自定义操作,例如我使用它向HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run添加某个脚本(以映射网络驱动器), 方法如下:

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

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.cmdunder 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\MapDrivenow has a Version under it.
  • 当用户下次登录时,Active Setup 会检查注册表中键HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive下是否存在值为 1 或更大的字符串 Version 。没有,因此它会创建它,并运行第二个 reg add 命令,该命令在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run下添加值为C:\map.cmd的字符串
  • 它只发生一次,因为在每次连续登录 Active Setup 时都会发现HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive现在下面有一个版本。

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

http://wpkg.org/Adding_Registry_Settings#Adding_entries_to_HKCU_for_all_users

回答by wunth

This is my process as I currently work in Windows 7

这是我的过程,因为我目前在 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.

  • 在 regedit 中的 HKCU 下找到您想要的密钥,然后将其导出。

  • 现在右键单击 HKEY_USERS 键并选择加载配置单元。浏览到 C:\Users\Default 并选择 NTUSER.DAT。随意命名蜂巢(例如 NAMEHERE)。

  • 在导出的 reg 文件中,将 [HKEY_CURRENT_USER\ 的所有实例替换为 [HKEY_USERS\NAMEHERE\(或任何您命名的配置单元)并保存文件

  • 双击 reg 文件以合并它

  • 突出显示 NAMEHERE 键并选择卸载配置单元。

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.

似乎这并不总是有效。我在这里寻找有关为什么这对我尝试在 HKEY_CURRENT_USER\Software\Classes\ 下添加的键不起作用的信息,但直到现在这个过程一直有效。如果我明白为什么这不起作用,我会添加一个注释 - 我目前想知道是否必须通过用户上下文中的策略添加它。

回答by TCugan

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

我认为最好的方法是通过 GPO。

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

创建一个 GPO,该 GPO 将修改HKCU应用您要对受该 GPO 影响的用户所做的更改。

The following Microsoft KB might help to implement this:

以下 Microsoft KB 可能有助于实现这一点:

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

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

回答by Alexey Ivanov

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.

或者唯一的方法是当该用户启动您的应用程序时。那就是您的应用程序在第一次启动时使用默认值创建密钥。