簡體   English   中英

當訪問Win 7 64上的注冊表時,ComRegisterFunction中的UnauthorizedAccessException

[英]UnauthorizedAccessException in ComRegisterFunction when accessing registry on Win 7 64

我有一個[ComRegisterFunction],用於注冊BHO Internet Explorer擴展。 在64位Windows 7計算機上注冊期間,在對subKey.SetValue(“ NoExplorer”,1)的調用上引發了UnauthorizedAccessException。

注冊表中似乎有BHO位於@ \\ HKLM \\ SOFTWARE \\ Wow6432Node \\ Microsoft \\ Windows \\ CurrentVersion \\ explorer \\ Browser Helper Objects,但是,嘗試在此注冊時,它們出現了相同的異常。 任何幫助,將不勝感激。

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

}

您需要以管理權限運行。

弄清楚了。 我必須添加以下內容才能使其正常運行。 不確定為什么它可以在其他版本的OS中使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM