繁体   English   中英

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

[英]Unable to read 64 bit registry key on Windows 7

我遇到以下尝试读取注册表项但失败的代码的问题。 特定的错误是:“ 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位视图中读取(如上面的代码所示-我已确认选择了RegistryView.Registry64)
  • 该代码以管理权限运行。 我已经通过上面检查WindowsBuiltInRole.Administrator的代码确认了这一点-日志中写入了“ Administrator”行而不是“ non-Administrator”行
  • 我尝试更改代码以读取字符串而不是字节,并且从其他位置(在HKLM而不是HKCU中)读取也没有成功。

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

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

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

现在,使用regedit命令转到注册表,找到子项“ CurrentVersion \\ TrayNotify”,您将看到创建的内容与要查找的内容之间的区别。

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

 echo %username%

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM