[英]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];
有关我的环境的一些事实:
我在这里错过明显的东西吗? 您可以提供的任何建议将不胜感激。 如果您还有其他问题需要解决,请告诉我。
如果您试图打开一个键,但是没有这样的键,您将得到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.