C# Windows service needs to make registry changes

筅森魡賤 提交于 2019-12-01 00:05:08

See this page:

A service that runs in the context of the LocalSystem account inherits the security context of the SCM...This has several implications:

The registry key HKEY_CURRENT_USER is associated with the default user, not the current user. To access another user's profile, impersonate the user, then access HKEY_CURRENT_USER.

What (who) do you expect Currentuser to be inside a service?

Registry is always fun, but doesn't that mean that you are editing the user registry settings of the "local system" user? So unless your actual user is logged in as "local system" (which I seriously doubt), they aren't going to see anything...

I suspect you need to edit the machine-wide settings, or the active user(s).

Run your service under another account that actually has a login.

The Local System account isn't a user, and therefore there is no Registry.CurrentUser key (no login = no current user = no current user key.

If it works outside of the service, it may be a permissions issue. Are you getting an exception when you try to use key.SetValue()? Is the service running under an account that has write access to the registry?

try putting the registry manipulation code in a try-catch block; services run in secondary threads, which eat exceptions

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