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

C# 无法在Windows 7上读取64位注册表项,c#,windows,registry,uac,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.Secur

我遇到了一个问题,下面的代码试图读取注册表项,但失败了。具体错误是:“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];
关于我的环境的一些事实:

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

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

如果您试图打开一个密钥,但没有这样的密钥,您将得到null。 尝试如下方式重新创建密钥:

if (mykey == null)
{
   key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"CurrentVersion\TrayNotify");
}
现在,使用regedit命令进入注册表,找到子项“CurrentVersion\t通知”,您将看到所创建内容与所查找内容之间的差异

如果要检查当前用户,请运行命令行,然后输入

 echo %username%

查看内部异常数据可能currentKey或myKey为null谢谢-我刚刚检查过,myKey确实为null(尽管currentUser不是)。但我已经确认,HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify的密钥确实存在。为什么我的代码不能看到它?你的当前用户是HKCU吗?我假设是。我如何确认这一点?