1
我試圖用UI編寫簡單的C#程序來在註冊表中添加我自己的密鑰和值。我將它添加到註冊表中,以便我的程序可以在稍後啓動時讀取它,並且不會自行重新配置。RegistryKeyPremissionsCheck和LocalMachine在當前上下文中不存在
RegistryKey rk = LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\AMC", RegistryKeyPremissionsCheck.ReadWriteSubTree, RegistryRights.ChangePermissions | RegistryRights.ReadKey);//Get the registry key desired with ChangePermissions Rights.
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule("Administrator", RegistryRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));//Create access rule giving full control to the Administrator user.
rk.SetAccessControl(rs); //Apply the new access rule to this Registry Key.
rk = LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\AMC", RegistryKeyPremissionsCheck.ReadWriteSubTree, RegistryRights.FullControl); // Opens the key again with full control.
rs.SetOwner(new NTAccount("Administrator"));// Set the securitys owner to be Administrator
rk.SetAccessControl(rs);// Set the key with the changed permission so Administrator is now owner.
這是我從Stackoverflow上的一些問題中選出的代碼。我試圖在添加/刪除/修改密鑰時解決權限問題。
我錯過了什麼?
非常感謝它的作品!它的工作.. LocalMachine現在是可用的。我想要的是爲系統上的用戶存儲密鑰對值。對另一個用戶來說,它將是註冊表中的另一棵樹。所以我想要的是在註冊表中添加幾個鍵並保存。請進一步幫助進一步.. –
我試過但權限錯誤來.. –
如果有幫助,我在Windows 7,32位上運行此。 –