2011-06-23 173 views
6

我在編寫應用程序以設置某些舊密鑰的權限時目前遇到問題。傳統的密鑰是相當鎖定,並實際上修改它們在註冊表中必須擁有所有權,然後添加自己完全控制。當試圖在代碼中複製它時,我無法獲得用於寫入的密鑰,並且出現「訪問被拒絕」錯誤。示例代碼:RegistrySecurity訪問被拒絕。 C#

RegistrySecurity rs = new RegistrySecurity(); 
rs.AddAccessRule(new RegistryAccessRule("Administrators", RegistryRights.FullControl, AccessControlType.Allow)); 
rs.SetOwner(new NTAccount("Administrators")); 
return LocalMachine.CreateSubKey(post, RegistryKeyPermissionCheck.ReadWriteSubTree, rs); 

任何想法將不勝感激。我也嘗試OpenSubKey與寫訪問請求,我只是無法得到密鑰。

謝謝你們。

回答

7

我終於找到了解決方案。您必須用「ChangePermissions」打開密鑰,然後更改自己的權限......然後用完全控制重新打開密鑰以更改所有者。 以下是操作方法。

RegistryKey rk = LocalMachine.OpenSubKey(subkey, 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(subkey, 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. 

這對我有用。讓我知道它是否適合你:)

如果你沒有以管理員身份登錄或者如果你需要其他用戶的權限,顯然可以將管理員更改爲其他用戶。

0

當您使用該代碼運行您的應用程序時,您是否右鍵單擊該exe文件並選擇「以管理員身份運行」?

+0

史蒂夫您好,我運行視覺工作室作爲管理員,以便調試應作爲管理員身份運行?我也嘗試以管理員身份運行,但仍然無法運行。 Legacy鍵完全鎖定,只能將System和Everyone設置爲只讀。我找到了一個解決方案,並會很快發佈。 – Skintkingle

0
using System.Security; 
using System.Security.AccessControl; 
using System.Security.Principal; 
using Microsoft.Win32; 

首先必須設定一個權限右完全訪問到子項

RegistryKey rkey = LocalMachine.OpenSubKey(_subKey, RegistryKeyPermissionCheck.ReadWriteSubTree, gistryRights.ChangePermissions); 
if (rkey == null) 
throw new Exception("Not Open"); 
//------- 
RegistrySecurity _registrySecurity =new RegistrySecurity();//Or rkey.GetAccessControl(); 
WindowsIdentity _windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); 
RegistryAccessRule _accessRule = new RegistryAccessRule(_windowsIdentity.Name, RegistryRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow); 
_registrySecurity.AddAccessRule(_accessRule); 
_registrySecurity.SetAccessRuleProtection(false, true); 
rkey.SetAccessControl(_registrySecurity); 

//--------Now, Set owner 

_registrySecurity.SetGroup(new NTAccount("Administrators")); //This is optional 
var SID = new System.Security.Principal.NTAccount("XXX\\Users"); 
_registrySecurity.SetOwner(SID); 
rkey.SetAccessControl(_registrySecurity); 

XXX:您的帳戶名

-2

微軟的Visual Studio 2015年(管理員)

_subKey = SOFTWARE\Wow6432Node\Norton

rkey.SetAccessControl(_registrySecurity);->「試圖按形成一個未經授權的操作。「`

+0

你到底在回答什麼?對不起,我不跟着。 – Skintkingle

-1
 RegistryKey rkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Norton\SecurityStatusSDK", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions); 
     if (rkey == null) 
      throw new Exception("Not Open"); 
     //------- 
     RegistrySecurity _registrySecurity = new RegistrySecurity();//Or rkey.GetAccessControl(); 
     WindowsIdentity _windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); 
     RegistryAccessRule _accessRule = new RegistryAccessRule(_windowsIdentity.Name, RegistryRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow); 
     _registrySecurity.AddAccessRule(_accessRule); 
     _registrySecurity.SetAccessRuleProtection(false, true); 
     try 
     { 
      rkey.SetAccessControl(_registrySecurity);// <---"Attempted to perform an unauthorized operation." 
     } 
     catch (UnauthorizedAccessException e) 
     { 

     } 

     //--------Now, Set owner 

     _registrySecurity.SetGroup(new NTAccount("Administrators")); //This is optional 
     var SID = new System.Security.Principal.NTAccount("XXX\\Users"); 
     _registrySecurity.SetOwner(SID); 
     rkey.SetAccessControl(_registrySecurity); 

我安裝了Norton Internet Security