problem save and reading from the registry

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aussie Rules

    #1

    problem save and reading from the registry

    Hi,

    I want to store some data in the registry, however I have not been able to
    do this, and think my logic maybe flawed.

    Firstly I try to open the registry and read in any existing values.
    Dim aKey As RegistryKey
    aKey = Registry.Curren tUser.OpenSubKe y("software\myA pplication")

    I then attempt to read in any existing values
    strServerName = aKey.GetValue(" ServerName")
    strLoginName = aKey.GetValue(" LoginName")

    When the first getValue is executed, i get an error 'object reference not
    set to an instance of an object', which I guess is fine as this registry
    entry doesn't exist yet.

    If the registry entry doesn't exist (say first time the application is run),
    then I capture the error on the above code and create the registry subkey=
    Registry.Curren tConfig.CreateS ubKey("software \myApplication" ) ' No error on
    this line

    After the user has logged in, i want to save the user id and server name to
    the registry, so that next time they use the program, step 1 will populate
    the login form with the last login details used.

    aKey.SetValue(" ServerName", frmSPlash.txtSe rver.Text)
    aKey.SetValue(" LoginName", frmSPlash.txtUs erID.Text)

    When the first setvalue is execute i get an error 'object reference not set
    to an instance of an object'. which i would not expect as it should be just
    creating the entry.

    What have I done wrong here......

    THanks



  • Izzy

    #2
    Re: problem save and reading from the registry

    Have you looked at the app.config file? This replaces storing settings
    in the registry for .NET apps. If you using VS 2005 it's very simple:

    'Get Value
    Value = My.Settings.[SettingsName]

    'Set Value
    My.Settings.[SettingsName] = Value


    Aussie Rules wrote:
    Hi,
    >
    I want to store some data in the registry, however I have not been able to
    do this, and think my logic maybe flawed.
    >
    Firstly I try to open the registry and read in any existing values.
    Dim aKey As RegistryKey
    aKey = Registry.Curren tUser.OpenSubKe y("software\myA pplication")
    >
    I then attempt to read in any existing values
    strServerName = aKey.GetValue(" ServerName")
    strLoginName = aKey.GetValue(" LoginName")
    >
    When the first getValue is executed, i get an error 'object reference not
    set to an instance of an object', which I guess is fine as this registry
    entry doesn't exist yet.
    >
    If the registry entry doesn't exist (say first time the application is run),
    then I capture the error on the above code and create the registry subkey=
    Registry.Curren tConfig.CreateS ubKey("software \myApplication" ) ' No error on
    this line
    >
    After the user has logged in, i want to save the user id and server name to
    the registry, so that next time they use the program, step 1 will populate
    the login form with the last login details used.
    >
    aKey.SetValue(" ServerName", frmSPlash.txtSe rver.Text)
    aKey.SetValue(" LoginName", frmSPlash.txtUs erID.Text)
    >
    When the first setvalue is execute i get an error 'object reference not set
    to an instance of an object'. which i would not expect as it should be just
    creating the entry.
    >
    What have I done wrong here......
    >
    THanks

    Comment

    • Peter Proost

      #3
      Re: problem save and reading from the registry

      I think the problem is that you read from a different location then you
      write to:

      Registry.Curren tUser.OpenSubKe y("software\myA pplication")
      Registry.Curren tConfig.CreateS ubKey("software \myApplication" )

      Below is a working sample, hope this helps:

      Greetz Peter

      Dim myKey As RegistryKey
      myKey = Registry.Curren tUser.OpenSubKe y("Software\tes tNG")
      If myKey Is Nothing Then
      myKey = Registry.Curren tUser.OpenSubKe y("Software", True)
      myKey.CreateSub Key("testNG")
      myKey = Registry.Curren tUser.OpenSubKe y("Software\tes tNG", True)
      myKey.SetValue( "testNG", "Peter Proost")
      Else
      MsgBox(myKey.Ge tValue("testNG" ))
      End If

      --
      Programming today is a race between software engineers striving to build
      bigger and better idiot-proof programs, and the Universe trying to produce
      bigger and better idiots. So far, the Universe is winning. (Rich Cook)

      "Aussie Rules" <AussieRules@no spam.nospamschr eef in bericht
      news:#tLdo66xGH A.3552@TK2MSFTN GP02.phx.gbl...
      Hi,
      >
      I want to store some data in the registry, however I have not been able to
      do this, and think my logic maybe flawed.
      >
      Firstly I try to open the registry and read in any existing values.
      Dim aKey As RegistryKey
      aKey = Registry.Curren tUser.OpenSubKe y("software\myA pplication")
      >
      I then attempt to read in any existing values
      strServerName = aKey.GetValue(" ServerName")
      strLoginName = aKey.GetValue(" LoginName")
      >
      When the first getValue is executed, i get an error 'object reference not
      set to an instance of an object', which I guess is fine as this registry
      entry doesn't exist yet.
      >
      If the registry entry doesn't exist (say first time the application is
      run),
      then I capture the error on the above code and create the registry subkey=
      Registry.Curren tConfig.CreateS ubKey("software \myApplication" ) ' No error
      on
      this line
      >
      After the user has logged in, i want to save the user id and server name
      to
      the registry, so that next time they use the program, step 1 will populate
      the login form with the last login details used.
      >
      aKey.SetValue(" ServerName", frmSPlash.txtSe rver.Text)
      aKey.SetValue(" LoginName", frmSPlash.txtUs erID.Text)
      >
      When the first setvalue is execute i get an error 'object reference not
      set
      to an instance of an object'. which i would not expect as it should be
      just
      creating the entry.
      >
      What have I done wrong here......
      >
      THanks
      >
      >
      >

      Comment

      • Jeffrey Tan[MSFT]

        #4
        RE: problem save and reading from the registry

        Hi Aussie,

        Additional to Peter's reply:

        What you got is the NullReferenceEx ception, which normally means that aKey
        is a null reference. The simplest way is set a breakpoint in aKey.SetValue
        line, and view aKey variable in Watch Window in VS.net IDE debugger.

        If once you find the null reference, you should check why aKey is not
        assigned a real object instance.

        Thanks.

        Best regards,
        Jeffrey Tan
        Microsoft Online Community Support
        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        http://msdn.microsoft.com/subscripti...ult.aspx#notif
        ications.

        Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
        where an initial response from the community or a Microsoft Support
        Engineer within 1 business day is acceptable. Please note that each follow
        up response may take approximately 2 business days as the support
        professional working with you may need further investigation to reach the
        most efficient resolution. The offering is not appropriate for situations
        that require urgent, real-time or phone-based interactions or complex
        project analysis and dump analysis issues. Issues of this nature are best
        handled working with a dedicated Microsoft Support Engineer by contacting
        Microsoft Customer Support Services (CSS) at
        http://msdn.microsoft.com/subscripti...t/default.aspx.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        Working...