UnauthorizedAccessException em ComRegisterFunction ao acessar registro em Win 7 64
-
19-09-2019 - |
Pergunta
Eu tenho um [ComRegisterFunction] que eu estou usando para registrar uma extensão explorer BHO Internet. Durante o registro no Windows de 64 bits 7 máquinas, um UnauthorizedAccessException é lançada sobre a chamada para subKey.SetValue ( "NoExplorer", 1).
O registro parece ter localizado @ \ HKLM \ SOFTWARE de BHO \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Browser Helper Objects, no entanto, eu levá-los mesma exceção ao tentar registrar lá. Qualquer ajuda seria apreciada.
[ComRegisterFunction]
public static void RegisterBho(Type type) {
string BhoKeyName= "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BhoKeyName, true) ??
Registry.LocalMachine.CreateSubKey(BhoKeyName);
if(registryKey == null) throw new ApplicationException("Unable to register Bho");
registryKey.Flush();
string guid = type.GUID.ToString("B");
RegistryKey subKey = registryKey.OpenSubKey(guid) ?? registryKey.CreateSubKey(guid);
if (subKey == null) throw new ApplicationException("Unable to register Bho");
subKey.SetValue("NoExplorer", 1);
registryKey.Close();
subKey.Close();
}
Solução 2
Descobri-lo. Eu tive que adicionar o seguinte para fazer o trabalho. Não tenho certeza por que ele trabalhou em outras versões do sistema operacional
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.FullControl,
InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow));
RegistryKey subKey = registryKey.OpenSubKey(guid) ?? registryKey.CreateSubKey(guid, RegistryKeyPermissionCheck.Default, rs);
Outras dicas
Você precisa executar com privilégios administrativos.