HKEY_CLASSES_ROOT中“不允许请求注册表访问”

时间:2015-08-23 16:42:06

标签: c# windows registry

我需要在 HKEY_CLASSES_ROOT和HKEY_LOCAL_MACHINE 中读取注册表值。但是当我试图获得具有读访问权限的子键对象时,我得到了这个例外:" 不允许请求注册表访问"。 (注意::在我的应用程序中,我没有对注册表进行任何编写或修改。

当我尝试从客户端计算机运行时,才会出现此错误。在我的机器上,它运作良好。 由于两台机器都具有管理访问权限,我认为还有其他一些问题导致问题。我可以从客户端计算机手动读取和编辑注册表值。

我编辑了清单文件,并将所需的权限从asInvoker更改为requireAdministrator。但没有运气。任何解决方法?

这是我的代码示例。

public List<SysApps> GetAllApps()
{
    List<SysApps> appList = new List<SysApps>();

    // The area's we are scanning
    appList.AddRange(this.LoadFiles(RegistryHive.CurrentUser, RegistryView.Registry32,
        @"Software\Microsoft\Windows\CurrentVersion\Uninstall"));
    appList.AddRange(this.LoadFiles(RegistryHive.LocalMachine, RegistryView.Registry32,
        @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"));
    appList.AddRange(this.LoadFiles(RegistryHive.LocalMachine, RegistryView.Registry64,
        @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"));

    return appList.OrderBy(r => r.DisplayName).ToList();
}

private List<SysApps> LoadFiles(RegistryHive reghive, RegistryView regview, string regkeypath)
{
    ist<SysApps> appList = new List<SysApps>();

    using (RegistryKey regedit = RegistryKey.OpenBaseKey(reghive, regview))
    using (RegistryKey regkey = regedit.OpenSubKey(regkeypath, RegistryKeyPermissionCheck.ReadSubTree))
    {
        if (regkey == null) goto last;

        foreach (string subkey in regkey.GetSubKeyNames())
        {
            if (subkey == null) continue;

            using (RegistryKey key = regkey.OpenSubKey(subkey))
            {
                if (key == null) continue;

                    SysApps sysapp = new SysApps();

                    sysapp._displayName = key.GetValue("DisplayName") == null ? string.Empty : (string)key.GetValue("DisplayName");
                    sysapp._publisher = key.GetValue("Publisher") == null ? string.Empty : (string)key.GetValue("Publisher");
                    sysapp._installDate = key.GetValue("InstallDate") == null ? string.Empty : (string)key.GetValue("InstallDate");

                    var decValue = key.GetValue("EstimatedSize") == null ? "0" : key.GetValue("EstimatedSize");
                    sysapp._estimatedSize = decValue.ToString() == "0" ? string.Empty : AppCommon.GetSize(decValue.ToString());

                    sysapp._displayIcon = key.GetValue("DisplayIcon") == null ? string.Empty : (string)key.GetValue("DisplayIcon");

                    if (string.IsNullOrEmpty(sysapp._displayIcon) && !string.IsNullOrEmpty(sysapp._displayName))
                        sysapp._displayIcon = this.GetIconForRoot(sysapp._displayName);


                    if (!string.IsNullOrEmpty(sysapp._displayIcon))
                        sysapp._displayIcon = sysapp._displayIcon.Replace("\"", string.Empty).Trim();

                    sysapp._displayVersion = key.GetValue("DisplayVersion") == null ? string.Empty : (string)key.GetValue("DisplayVersion");
                    sysapp._uninstallString = key.GetValue("UninstallString") == null ? string.Empty : (string)key.GetValue("UninstallString");
                    sysapp._modifyPath = key.GetValue("ModifyPath") == null ? string.Empty : (string)key.GetValue("ModifyPath");

                    // Validate
                    var rType = (string)key.GetValue("ReleaseType");
                    var sComponent = key.GetValue("SystemComponent");
                    var pName = (string)key.GetValue("ParentDisplayName");

                    if (!string.IsNullOrEmpty(sysapp._displayName) && string.IsNullOrEmpty(rType) && string.IsNullOrEmpty(pName) && (sComponent == null))
                    {
                        AppCommon.FileSize += Int32.Parse(decValue.ToString());
                        appList.Add(sysapp);
                    }

                    key.Flush();
                    key.Close();
                }
            }

            regkey.Flush();
            regkey.Close();
        }

    last:
        return appList;
}

private string GetIconForRoot(string productName)
{
    string result = string.Empty;
    string installerKey = @"Installer\Products";

    bool isFound = false;

    using (RegistryKey regedit = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32))
    using (RegistryKey regkey = regedit.OpenSubKey(installerKey, RegistryKeyPermissionCheck.ReadSubTree))
    {
        if (regkey == null) goto last;

        foreach (string subkey in regkey.GetSubKeyNames())
        {
            if (subkey == null) continue;

            using (RegistryKey key = regkey.OpenSubKey(subkey))
            {
                if (key.GetValue("ProductName") != null)
                    if (productName == key.GetValue("ProductName").ToString())
                        if (key.GetValue("ProductIcon") != null)
                        {
                            isFound = true;
                            result = key.GetValue("ProductIcon").ToString();
                        }

                    key.Flush();
                    key.Close();

                    if (isFound) break;
            }
        }

        egkey.Flush();
        regkey.Close();
    }

    last:
        return result;
}

0 个答案:

没有答案
相关问题