C#在所有用户的注册表中安全写入的位置

时间:2016-07-13 08:29:21

标签: c# registry

我有一个Windows服务,它在注册表中写入了一些这样的参数。 我希望这些参数对任何用户都是通用的,无论Windows服务“RunAs”是什么意思。

你在哪里建议写它们?

我尝试在SOFTWARE \ MyWindowsService \ Server中编写它们但我遇到了权限问题,您可以在此代码中看到。 例如,我尝试在SOFTWARE \ MyWindowsService \ Server \ Key1中读取和写入,Key1的类型是字符串(REG_SZ)......

//Get current user for windows Service
_windowsIdentity = WindowsIdentity.GetCurrent().Name;
MessageBox.Show("Try to change registry permissions for" + _windowsIdentity); //==> LocalSystem is my current account

//Get Main windows service registry
_WindowsServiceRegKeys = Registry.LocalMachine.CreateSubKey("SOFTWARE\\THIS_WINDOWS_SERVICE_FOLDER\\SERVER", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistrySecurity rs = new RegistrySecurity();
rs = _WindowsServiceRegKeys.GetAccessControl();

//Add new permissions
rs.AddAccessRule(new RegistryAccessRule(_windowsIdentity
                         , RegistryRights.FullControl
                         , InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
                         , PropagationFlags.InheritOnly, AccessControlType.Allow));
_WindowsServiceRegKeys.SetAccessControl(rs);

//Try to read a var
object objRetRegistryKeyValue = _WindowsServiceRegKeys.GetValue("Key1"); //ERROR: object is null

_WindowsServiceRegKeys.SetValue("Key1", "Test", RegistryValueKind.String);

你可以帮我解决这个错误吗?

谢谢和问候,

0 个答案:

没有答案