2013-07-29 80 views
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上的一些問題中選出的代碼。我試圖在添加/刪除/修改密鑰時解決權限問題。

我錯過了什麼?

回答

2

你正在使用的第一行RegistryKeyPremissionsCheck這是你尚未定義的變量。 LocalMachine。 寫行

RegistryKey LocalMachine = Registry.LocalMachine; 

您的LocalMachine

RegistryKeyPremissionsCheck.ReadWriteSubTree如果你想你要打開的子鍵下編輯值與true替換代碼之前,否則,你可以把false

如果您只是想將註冊表項添加到註冊表中,我只會使用 -

int MyNumber = 0; // Your value, doesnt have to be a number 
Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\AMC", "My User Name", MyNumber); 

,並獲得價值

object val = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\AMC", "My User Name", -1); 

現在我把這個代碼,如果你有正確的權限(你是管理員)

RegistryKey LocalMachine = Registry.LocalMachine; 
     RegistryKey rk = LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\AMC", 
      RegistryKeyPermissionCheck.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", 
      RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl); // Opens the key again with full control. 
     rs.SetOwner(new NTAccount("Administrator"));// Set the securities owner to be Administrator 
     rk.SetAccessControl(rs); 
     int MyNumber = 0; // Your value, doesn't have to be a number 
     rk.SetValue("username", MyNumber);// The username should by the dynamic part 
+0

非常感謝它的作品!它的工作.. LocalMachine現在是可用的。我想要的是爲系統上的用戶存儲密鑰對值。對另一個用戶來說,它將是註冊表中的另一棵樹。所以我想要的是在註冊表中添加幾個鍵並保存。請進一步幫助進一步.. –

+0

我試過但權限錯誤來.. –

+0

如果有幫助,我在Windows 7,32位上運行此。 –

相關問題