title: Unable to read 64 bit registry key on Windows 7
body: <p>I'm having an issue with the following bit of code that attempts to read a registry key but fails. The specific error is: "<em>System.NullReferenceException: Object reference not set to an instance of an object</em>." The code I'm using is below:</p>
  
  <pre><code>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];
  </code></pre>
  
  <p>Some facts about my environment:</p>
  
  <ul>
  <li>This is a 32-bit app that I'm running on Windows 7 (with UAC enabled - no, I can't turn it off). However, I am reading from the 64-bit view of the registry (as seen in the code above - I've confirmed that RegistryView.Registry64 is being selected)</li>
  <li>The code is running with Administrative rights. I've confirmed this through the code above that checks WindowsBuiltInRole.Administrator - the log writes the "Administrator" line and not the "non-Administrator" line</li>
  <li>I've tried changing the code to read a string rather than a byte, and also to read from a different location (in HKLM rather than HKCU) with no success.</li>
  </ul>
  
  <p>Am I missing something obvious here? Any advice you can give would be much appreciated. If there's anything else you need to troubleshoot, just let me know.</p>