[英]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.