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



我遇到了一个问题,下面的代码试图读取注册表项,但失败。具体的错误是:"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(@"SoftwareClassesLocal SettingsSoftwareMicrosoftWindowsCurrentVersionTrayNotify", 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位的应用程序,我在Windows 7上运行(启用了UAC -不,我不能关闭它)。但是,我是从注册表的64位视图读取的(如上面的代码所示)-我已经确认了RegistryView。Registry64正在被选中)
  • 代码正在以管理员权限运行。我已经通过上面检查WindowsBuiltInRole的代码确认了这一点。管理员-日志写入"管理员"行,而不是"非管理员"行
  • 我试过改变代码来读取字符串而不是字节,也从不同的位置读取(在HKLM而不是HKCU),没有成功。

我错过了什么明显的东西吗?你能给的任何建议我都会很感激。

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

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

现在使用regedit命令进入注册表,找到子键"CurrentVersionTrayNotify",您将看到您创建的内容与您正在寻找的内容之间的差异。

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

 echo %username%

最新更新