簡體   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