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. Fail to read simple string from the registry ( code attached )

Fail to read simple string from the registry ( code attached )

Scheduled Pinned Locked Moved C#
windows-admindebugging
8 Posts 3 Posters 2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Try to read string from the registry - i get beck null all the time Code:

    public static string ReadStringFromRegistry( string hive, string key )
    {
    string strRetVal = null;

            try
            {
    
                using( RegistryKey regKey = Registry.LocalMachine.OpenSubKey( hive ) )
                {
                    if( null != regKey )
                    {
                        strRetVal = regKey.GetValue( key ) as string;
    
                        if( String.IsNullOrEmpty( strRetVal ) )
                        {
                            strRetVal = null;
                            Debug.Assert( false, "fail to read from the registry" );
                        }
                    }
                }
            }
            catch( Exception e )
            {
    
            }
    
            
            return strRetVal;
        }
    
    J L 2 Replies Last reply
    0
    • L Lost User

      Try to read string from the registry - i get beck null all the time Code:

      public static string ReadStringFromRegistry( string hive, string key )
      {
      string strRetVal = null;

              try
              {
      
                  using( RegistryKey regKey = Registry.LocalMachine.OpenSubKey( hive ) )
                  {
                      if( null != regKey )
                      {
                          strRetVal = regKey.GetValue( key ) as string;
      
                          if( String.IsNullOrEmpty( strRetVal ) )
                          {
                              strRetVal = null;
                              Debug.Assert( false, "fail to read from the registry" );
                          }
                      }
                  }
              }
              catch( Exception e )
              {
      
              }
      
              
              return strRetVal;
          }
      
      J Offline
      J Offline
      JF2015
      wrote on last edited by
      #2

      Your code is working fine for me, so there is nothing wrong with it. You probably have wrong input strings. Please check if "hive" and "key" are correct.

      L 1 Reply Last reply
      0
      • J JF2015

        Your code is working fine for me, so there is nothing wrong with it. You probably have wrong input strings. Please check if "hive" and "key" are correct.

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

        Thanks, In the line strRetVal = regKey.GetValue( key ) as string; i get strRetVal as null.

        J 2 Replies Last reply
        0
        • L Lost User

          Thanks, In the line strRetVal = regKey.GetValue( key ) as string; i get strRetVal as null.

          J Offline
          J Offline
          JF2015
          wrote on last edited by
          #4

          So, what is the value that you expect to read? And of which type is it?

          L 1 Reply Last reply
          0
          • L Lost User

            Thanks, In the line strRetVal = regKey.GetValue( key ) as string; i get strRetVal as null.

            J Offline
            J Offline
            JF2015
            wrote on last edited by
            #5

            You can also see this site that explains how to use the .GetValue() method with different registry value types: http://msdn.microsoft.com/en-us/library/fdf576x1%28VS.80%29.aspx[^]

            1 Reply Last reply
            0
            • J JF2015

              So, what is the value that you expect to read? And of which type is it?

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

              I read simple string that i created. If i try to read some other string ( that i did not created ) from the next hive - i see that the result are ok. Very Strange... P.S: using win7 - and i check the premmision of the registry access.

              J 1 Reply Last reply
              0
              • L Lost User

                I read simple string that i created. If i try to read some other string ( that i did not created ) from the next hive - i see that the result are ok. Very Strange... P.S: using win7 - and i check the premmision of the registry access.

                J Offline
                J Offline
                JF2015
                wrote on last edited by
                #7

                Have you checked in the registry editor if your created string is somehow different from the one you are able to read? (is it of type REG_SZ?) I just tested creating one key and then read it back and it worked fine with XP.

                1 Reply Last reply
                0
                • L Lost User

                  Try to read string from the registry - i get beck null all the time Code:

                  public static string ReadStringFromRegistry( string hive, string key )
                  {
                  string strRetVal = null;

                          try
                          {
                  
                              using( RegistryKey regKey = Registry.LocalMachine.OpenSubKey( hive ) )
                              {
                                  if( null != regKey )
                                  {
                                      strRetVal = regKey.GetValue( key ) as string;
                  
                                      if( String.IsNullOrEmpty( strRetVal ) )
                                      {
                                          strRetVal = null;
                                          Debug.Assert( false, "fail to read from the registry" );
                                      }
                                  }
                              }
                          }
                          catch( Exception e )
                          {
                  
                          }
                  
                          
                          return strRetVal;
                      }
                  
                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  when you swallow exceptions like you do, you deserve the trouble you're getting. X|

                  Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                  Season's Greetings to all CPians.

                  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