Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Registry.OpenSubKey Returns Null

Registry.OpenSubKey Returns Null

Scheduled Pinned Locked Moved C#
visual-studiocomwindows-adminhelpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm trying to read in a list of VS2010 Projects from the registry:

    private const string VS_PROJECT_MRU_PATH = @"Software\Microsoft\VisualStudio\10.0\ProjectMRUList";

    public static List GetSubKeyNodeNames(BaseKey BaseKey, string SubKey)
    {
    List retVal = null;

    RegistryKey baseKey;
    
    if (SubKey != "")
    {
        baseKey = \_GetRegistryBaseKey(BaseKey);
        baseKey = baseKey.OpenSubKey(SubKey, false);  //<===== FAILS HERE - basekey is null
    }
    else
    {
        baseKey = \_GetRegistryBaseKey(BaseKey);
    }
    
    
    if (baseKey == null && SubKey != "")
    {
        throw new Exception("Registry subkey not found");
    }
    else
    {
        string\[\] names = baseKey.GetValueNames();
    
        retVal = (from n in names
                    select n).ToList();
    }
    
    return retVal;
    

    }

    I have verified that the key DOES indeed exist. I just doesn't open. The problem is similar to this[^] except that I'm reading from HKEY_CURRENT_USER, not HKEY_LOCAL_MACHINE. Anyone know what;s going on here? Thanks

    Everything makes sense in someone's mind

    J L 2 Replies Last reply
    0
    • K Kevin Marois

      I'm trying to read in a list of VS2010 Projects from the registry:

      private const string VS_PROJECT_MRU_PATH = @"Software\Microsoft\VisualStudio\10.0\ProjectMRUList";

      public static List GetSubKeyNodeNames(BaseKey BaseKey, string SubKey)
      {
      List retVal = null;

      RegistryKey baseKey;
      
      if (SubKey != "")
      {
          baseKey = \_GetRegistryBaseKey(BaseKey);
          baseKey = baseKey.OpenSubKey(SubKey, false);  //<===== FAILS HERE - basekey is null
      }
      else
      {
          baseKey = \_GetRegistryBaseKey(BaseKey);
      }
      
      
      if (baseKey == null && SubKey != "")
      {
          throw new Exception("Registry subkey not found");
      }
      else
      {
          string\[\] names = baseKey.GetValueNames();
      
          retVal = (from n in names
                      select n).ToList();
      }
      
      return retVal;
      

      }

      I have verified that the key DOES indeed exist. I just doesn't open. The problem is similar to this[^] except that I'm reading from HKEY_CURRENT_USER, not HKEY_LOCAL_MACHINE. Anyone know what;s going on here? Thanks

      Everything makes sense in someone's mind

      J Offline
      J Offline
      JOAT MON
      wrote on last edited by
      #2

      I don't know what's going on, but have you stepped through your function _GetRegistryBaseKey(BaseKey) to see why it returns null?

      Jack of all trades ~ Master of none.

      1 Reply Last reply
      0
      • K Kevin Marois

        I'm trying to read in a list of VS2010 Projects from the registry:

        private const string VS_PROJECT_MRU_PATH = @"Software\Microsoft\VisualStudio\10.0\ProjectMRUList";

        public static List GetSubKeyNodeNames(BaseKey BaseKey, string SubKey)
        {
        List retVal = null;

        RegistryKey baseKey;
        
        if (SubKey != "")
        {
            baseKey = \_GetRegistryBaseKey(BaseKey);
            baseKey = baseKey.OpenSubKey(SubKey, false);  //<===== FAILS HERE - basekey is null
        }
        else
        {
            baseKey = \_GetRegistryBaseKey(BaseKey);
        }
        
        
        if (baseKey == null && SubKey != "")
        {
            throw new Exception("Registry subkey not found");
        }
        else
        {
            string\[\] names = baseKey.GetValueNames();
        
            retVal = (from n in names
                        select n).ToList();
        }
        
        return retVal;
        

        }

        I have verified that the key DOES indeed exist. I just doesn't open. The problem is similar to this[^] except that I'm reading from HKEY_CURRENT_USER, not HKEY_LOCAL_MACHINE. Anyone know what;s going on here? Thanks

        Everything makes sense in someone's mind

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Where is the method _GetRegistryBaseKey() defined and what value is it supposed to return?

        Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

        K 1 Reply Last reply
        0
        • L Lost User

          Where is the method _GetRegistryBaseKey() defined and what value is it supposed to return?

          Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #4

          Here it is. This part works.

          private static RegistryKey _GetRegistryBaseKey(BaseKey BaseKey)
          {
          RegistryKey key = null; ;

          switch (BaseKey)
          {
              case BaseKey.HKEY\_CLASSES\_ROOT:
                  key = Registry.ClassesRoot;
                  break;
          
              case BaseKey.HKEY\_CURRENT\_CONFIG:
                  key = Registry.CurrentConfig;
                  break;
          
              case BaseKey.HKEY\_CURRENT\_USER:
                  key = Registry.CurrentUser;
                  break;
          
              case BaseKey.HKEY\_DYN\_DATA:
                  key = Registry.DynData;
                  break;
          
              case BaseKey.HKEY\_LOCAL\_MACHINE:
                  key = Registry.LocalMachine;
                  break;
          
              case BaseKey.HKEY\_PERFORMANCE\_DATA:
                  key = Registry.PerformanceData;
                  break;
          }
          
          return key;
          

          }

          Everything makes sense in someone's mind

          S L 2 Replies Last reply
          0
          • K Kevin Marois

            Here it is. This part works.

            private static RegistryKey _GetRegistryBaseKey(BaseKey BaseKey)
            {
            RegistryKey key = null; ;

            switch (BaseKey)
            {
                case BaseKey.HKEY\_CLASSES\_ROOT:
                    key = Registry.ClassesRoot;
                    break;
            
                case BaseKey.HKEY\_CURRENT\_CONFIG:
                    key = Registry.CurrentConfig;
                    break;
            
                case BaseKey.HKEY\_CURRENT\_USER:
                    key = Registry.CurrentUser;
                    break;
            
                case BaseKey.HKEY\_DYN\_DATA:
                    key = Registry.DynData;
                    break;
            
                case BaseKey.HKEY\_LOCAL\_MACHINE:
                    key = Registry.LocalMachine;
                    break;
            
                case BaseKey.HKEY\_PERFORMANCE\_DATA:
                    key = Registry.PerformanceData;
                    break;
            }
            
            return key;
            

            }

            Everything makes sense in someone's mind

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            Works fine on my machine compiled as x86, x64 and Any CPU. Does your app run in restricted mode or is it a silverlight / web app or something? Does it run under a different user? Such as a service, web service, etc? Seems like a security / permission problem to me.

            K 1 Reply Last reply
            0
            • K Kevin Marois

              Here it is. This part works.

              private static RegistryKey _GetRegistryBaseKey(BaseKey BaseKey)
              {
              RegistryKey key = null; ;

              switch (BaseKey)
              {
                  case BaseKey.HKEY\_CLASSES\_ROOT:
                      key = Registry.ClassesRoot;
                      break;
              
                  case BaseKey.HKEY\_CURRENT\_CONFIG:
                      key = Registry.CurrentConfig;
                      break;
              
                  case BaseKey.HKEY\_CURRENT\_USER:
                      key = Registry.CurrentUser;
                      break;
              
                  case BaseKey.HKEY\_DYN\_DATA:
                      key = Registry.DynData;
                      break;
              
                  case BaseKey.HKEY\_LOCAL\_MACHINE:
                      key = Registry.LocalMachine;
                      break;
              
                  case BaseKey.HKEY\_PERFORMANCE\_DATA:
                      key = Registry.PerformanceData;
                      break;
              }
              
              return key;
              

              }

              Everything makes sense in someone's mind

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Based on your previous message, this cannot be true, since you stated that the key returned from this method is null. You need to use your debugger to see what value is being passed in to your code in the first place.

              Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

              K 1 Reply Last reply
              0
              • L Lost User

                Based on your previous message, this cannot be true, since you stated that the key returned from this method is null. You need to use your debugger to see what value is being passed in to your code in the first place.

                Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #7

                I said OpenSubKey is returning False. _GetRegistryBaseKey return the correct base key. Then the call to OpenSubKey returns False.

                Everything makes sense in someone's mind

                L 1 Reply Last reply
                0
                • S SledgeHammer01

                  Works fine on my machine compiled as x86, x64 and Any CPU. Does your app run in restricted mode or is it a silverlight / web app or something? Does it run under a different user? Such as a service, web service, etc? Seems like a security / permission problem to me.

                  K Offline
                  K Offline
                  Kevin Marois
                  wrote on last edited by
                  #8

                  SledgeHammer01 wrote:

                  Seems like a security / permission problem to me.

                  That's what I thought, but I'm running as an admin, with no restrictions that I know of. It's a WPF app.

                  Everything makes sense in someone's mind

                  S 1 Reply Last reply
                  0
                  • K Kevin Marois

                    SledgeHammer01 wrote:

                    Seems like a security / permission problem to me.

                    That's what I thought, but I'm running as an admin, with no restrictions that I know of. It's a WPF app.

                    Everything makes sense in someone's mind

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #9

                    Do you have a try / catch hiding exceptions at some level? That method throws an exception on errors. Looking at the .Net source, the only way you should get a null is if the Win32 native function is returning an error other then security. Unfortunately, .Net hides the real error code from you. You might want to call the native RegOpenKeyEx through interop so you can get the real error code.

                    1 Reply Last reply
                    0
                    • K Kevin Marois

                      I said OpenSubKey is returning False. _GetRegistryBaseKey return the correct base key. Then the call to OpenSubKey returns False.

                      Everything makes sense in someone's mind

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Well my previous comment still stands; you need to use your debugger to check all values to determine why OpenSubKey() returns null. With the information you have provided it's impossible to guess what values exist within your application.

                      Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups