C# 在Win 7 64上访问注册表时,ComRegisterFunction中的UnauthorizedAccessException

C# 在Win 7 64上访问注册表时,ComRegisterFunction中的UnauthorizedAccessException,c#,interop,registry,com-interop,browser-extension,C#,Interop,Registry,Com Interop,Browser Extension,我使用[ComRegisterFunction]注册BHO Internet explorer扩展。在64位windows 7计算机上注册期间,未经授权的数据访问异常 在调用subKey.SetValue(“NoExplorer”,1)时抛出 注册表中似乎有BHO的@\HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\explorer\Browser Helper对象,但是,我在尝试注册时遇到了相同的异常。任何帮助都将不胜感激

我使用[ComRegisterFunction]注册BHO Internet explorer扩展。在64位windows 7计算机上注册期间,未经授权的数据访问异常 在调用subKey.SetValue(“NoExplorer”,1)时抛出

注册表中似乎有BHO的@\HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\explorer\Browser Helper对象,但是,我在尝试注册时遇到了相同的异常。任何帮助都将不胜感激

[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();

}

您需要以管理权限运行。

解决了这个问题。我必须添加以下内容才能使其正常工作。不知道为什么它能在其他版本的操作系统中工作

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);

是的,但您已启用UAC。右键单击,然后单击以管理员身份运行。