c# - 无法在 Windows 7 上读取 64 位注册表项

标签 c# windows registry uac

我遇到了以下尝试读取注册表项但失败的代码的问题。具体错误是:“System.NullReferenceException:未将对象引用设置为对象的实例。”我使用的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Collections;
using System.Diagnostics;
using System.Security.Principal;

namespace UDTLibrary
{
    public class NotificationBar
    {
        public static void Main(string[] args)
        {
            //Get User Info
            string sSource;
            string sLog;

            sSource = "TestCSFileSysWatcher";
            sLog = "Application";

            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);

            EventLog.WriteEntry(sSource, "NotificationBar.Main start");

            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                EventLog.WriteEntry(sSource, "NotificationBar.Main - non-Administrator");
            }
            else
            {
                EventLog.WriteEntry(sSource, "NotificationBar.Main Administrator");
            }

            NotificationBar p1 = new NotificationBar();
            string prName = null;
            int value = 0;
            if (args == null)
            {
                throw new Exception("Attempt to run NotificationBar with no arguments supplied.");
            }
            else
            {
                if (args.Length != 2)
                {
                    throw new Exception("Wrong number of arguments supplied.");
                }
                else
                {
                    prName = args[0];
                    value = Convert.ToInt32(args[1]);
                }
            }

            RegistryKey currentUser = null;
            if (Environment.Is64BitOperatingSystem)
            {
                currentUser = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64);
            }
            else
            {
                currentUser = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32);
            }
            RegistryKey myKey = currentUser.OpenSubKey(@"Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify", true);
            byte[] all = (byte[])myKey.GetValue("IconStreams"); //here is where the code fails
            byte[] allwithoutheader = new byte[all.Length - 20];
            byte[] header = new byte[20];

关于我的环境的一些事实:

  • 这是我在 Windows 7 上运行的 32 位应用程序(启用了 UAC - 不,我无法将其关闭)。但是,我正在读取注册表的 64 位 View (如上面的代码所示 - 我已确认已选择 RegistryView.Registry64)
  • 代码以管理权限运行。我已经通过上面检查 WindowsBuiltInRole.Administrator 的代码确认了这一点 - 日志写入“管理员”行而不是“非管理员”行
  • 我曾尝试更改代码以读取字符串而不是字节,并尝试从不同的位置(在 HKLM 而不是 HKCU)读取,但没有成功。

我在这里遗漏了什么明显的东西吗?您能提供的任何建议将不胜感激。如果您还有其他问题需要解决,请告诉我。

最佳答案

如果您尝试打开一个 key ,但没有这样的 key ,您将得到 null。 尝试像这样重新创建 key :

if (mykey == null)
{
   key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"CurrentVersion\TrayNotify");
}

现在使用 regedit 命令进入注册表并找到您的子键“CurrentVersion\TrayNotify”,您将看到您创建的内容和您正在寻找的内容之间的区别..

如果你想查看你当前的用户,运行命令行然后输入

 echo %username%

关于c# - 无法在 Windows 7 上读取 64 位注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110320/

相关文章:

c# - 如何使用 LINQ 对对象集合进行简单的基于顺序的验证?

c# - 我在哪里可以下载 MonoMac 的编译版本?

.net - 从 Windows 通过 Entity Framework 连接到 Informix 的问题

c# - Registry.LocalMachine 和 RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default) 之间有什么区别

c# - 修改字典后抛出异常

带有 MahApps 的 C# WPF 应用程序无法在 Windows 2008 中显示

windows - 如何高效地从注册表中读取GUID?

windows - 读取存储在注册表中的 GUID 值

c++ - 识别 Windows 中连接的驱动器

c# - 从 mysql 填充数据网格特定定义的列