我有一个服务需要每隔5分钟更新注册表(抵消gpo)。代码在常规应用程序中运行良好,但是当我将它放入Windows服务中时,它不会进行更改。我使用的是本地系统帐户的服务,它不抛出任何异常C#Windows服务需要进行注册表更改
下面的代码在一个普通的控制台应用程序而不是在服务:
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");
什么想法?
编辑:
感谢您的答复。我不知道为什么“当前用户”逃脱了我的注意。感谢您指出了这一点。
我的问题仍然是,组策略被推向当前用户。现在我正在考虑创建一个在启动时加载并保持活动状态的应用程序。任何其他建议将受到欢迎。
编辑:
至于威尔的评论,我无法找到在Win32 API中的UpdatePerUserSystemParameters函数签名。这是否意味着它可以在没有参数的情况下被调用?
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UpdatePerUserSystemParameters();
实际上,本地系统/本地服务/网络服务帐户得到了他们的HKCU条目。只需检查HK_USERS即可。 –