C#Windows服务需要进行注册表更改 [英] C# Windows service needs to make registry changes

查看:49
本文介绍了C#Windows服务需要进行注册表更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务,需要每5分钟更新一次注册表(counteract gpo).该代码在常规应用程序中可以正常运行,但是当我将其放在Windows服务中时,它不会进行更改.我正在使用本地系统帐户提供服务,并且未引发任何异常

I have a service that needs to update the registry every 5 minutes (counteract gpo). The code runs fine in a regular application, but when I put it in a windows service, it doesn't make the changes. I am using the local system account for the service and it is not throwing any exceptions

以下代码可在常规控制台应用程序中使用,但不能在服务中使用:

The code below works in a regular console app but not in the service:

RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        if (key != null)
        {
            key.SetValue("ScreenSaverIsSecure", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveActive", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveTimeOut", "0", RegistryValueKind.String);
            key.SetValue("SCRNSAVE.EXE", "", RegistryValueKind.String);
        }


        key = Registry.CurrentUser.OpenSubKey(@"Software\Policies\Microsoft\Windows\Control Panel\Desktop", true);
        if (key != null)
        {
            key.SetValue("ScreenSaverIsSecure", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveActive", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveTimeOut", "0", RegistryValueKind.String);
            key.SetValue("SCRNSAVE.EXE", "", RegistryValueKind.String);
        }
        System.Diagnostics.Process.Start(@"c:\windows\System32\RUNDLL32.EXE", "user32.dll, UpdatePerUserSystemParameters");

有什么想法吗?

感谢您的答复.我不知道为什么"CurrentUser"引起了我的注意.感谢您指出这一点.

Thanks for the replies. I don't know why the "CurrentUser" escaped my attention. Thanks for pointing that out.

我的问题仍然是,针对当前用户推送了组策略.现在,我正在考虑只创建一个在启动时加载并保持活动状态的应用程序.任何其他建议都将受到欢迎.

My problem still remains that the group policy is being pushed against the currentuser. Right now I am considering just creating an app that loads at startup and stays active. Any other suggestions would be welcome.

关于Will的评论,我无法在win32 api中找到 UpdatePerUserSystemParameters 函数签名.这是否意味着可以在不带参数的情况下调用它?

As to Will's comment, I am unable to find the UpdatePerUserSystemParameters function signature in the win32 api. Does that imply that it can be called without parameters?

   [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool UpdatePerUserSystemParameters();

推荐答案

请参见此页面:

在LocalSystem帐户的上下文中运行的服务会继承SCM的安全性上下文...这有几个含义:

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

注册表项HKEY_CURRENT_USER与默认用户关联,而不与当前用户关联.要访问其他用户的个人资料,请模拟该用户,然后访问HKEY_CURRENT_USER.

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.

这篇关于C#Windows服务需要进行注册表更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆