Unable to write to registry

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vernon Peppers

    #1

    Unable to write to registry

    I am a newbie with .NET, trying to step up from VB6. I have a need to write
    to and read from the registry. Config files will not be acceptable due to
    their lack of security. The registry entry will have to do with the proper
    registration and purchase of the program. I have the following code in my
    program:

    Public Sub SetRegValue(ByV al ValueName As String, ByVal TestValue As
    Boolean)

    Dim f As New RegistryPermiss ion(RegistryPer missionAccess.R ead Or
    RegistryPermiss ionAccess.Write , "HKEY_LOCAL_MAC HINE\Software")

    Application.Com monAppDataRegis try.SetValue(Va lueName, TestValue)

    End Sub

    I receive the following error whenever I try to run the program:
    Unhandled Exception: System.Security .SecurityExcept ion: Request for the
    permission of type System.Security .Permissions.Re gistryPermissio n, mscorlib,
    Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9 failed.

    I am an administrator on my computer, so that can't be the problem. What am
    I doing wrong?


  • iwdu15

    #2
    RE: Unable to write to registry

    if you just want to write strings, use the "Savesettin g" and "getsetting "
    features

    ex:

    SaveSetting("Pr ogram", "Section", "Key", "Setting")

    Dim temp as string
    temp = GetSetting("Pro gram", "Section", "Key")

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Unable to write to registry

      Vernon,

      Why are you trying to set the hardware settings for a program.

      Setting things in the curren user part of the registry is very easy with
      VBNet.

      http://msdn.microsoft.com/library/de...classtopic.asp

      The only difficult thing is to find the format of the stringarray. However
      you are using plain strings, so using this link you would be able to handle
      your problem in my opinion.

      I hope this helps,

      Cor


      Comment

      • Vernon Peppers

        #4
        Re: Unable to write to registry

        I am not trying to set hardware settings. I need program settings for
        whomever is using the machine. HKCU is different for each user. I would
        have to set the program security features for each user when he accessed it
        the first time. The program will be sold on the Internet, and will allow a
        demo mode for thirty days. After that the program must be purchased and
        registered, or it quits working. I need to store the registration data in a
        secure place where it can be accessed by all users. HKLM is the proper
        place for that kind of data.

        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
        news:u$7iYO7pFH A.3160@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Vernon,
        >
        > Why are you trying to set the hardware settings for a program.
        >
        > Setting things in the curren user part of the registry is very easy with
        > VBNet.
        >
        >[/color]
        http://msdn.microsoft.com/library/de...classtopic.asp[color=blue]
        >
        > The only difficult thing is to find the format of the stringarray. However
        > you are using plain strings, so using this link you would be able to[/color]
        handle[color=blue]
        > your problem in my opinion.
        >
        > I hope this helps,
        >
        > Cor
        >
        >[/color]


        Comment

        • Vernon Peppers

          #5
          Re: Unable to write to registry

          Savesetting and Getsetting saves everything in the VB and VBA key. Besides,
          that doesn't solve my prolbem with registry permissions. That's what I need
          help on.

          "iwdu15" <iwdu15@discuss ions.microsoft. com> wrote in message
          news:47BC0495-AFCE-49F6-A09E-EE7ABD36940C@mi crosoft.com...[color=blue]
          > if you just want to write strings, use the "Savesettin g" and "getsetting "
          > features
          >
          > ex:
          >
          > SaveSetting("Pr ogram", "Section", "Key", "Setting")
          >
          > Dim temp as string
          > temp = GetSetting("Pro gram", "Section", "Key")[/color]


          Comment

          • Vernon Peppers

            #6
            Re: Unable to write to registry

            As i continue to work, I find that I not only can not set permissions for
            registry access, I can't set permissions for file IO. Someone please help
            me with this permissions problem.

            "Vernon Peppers" <procomp@commun ity.nospam> wrote in message
            news:eYm%23vXzp FHA.1044@tk2msf tngp13.phx.gbl. ..[color=blue]
            > I am a newbie with .NET, trying to step up from VB6. I have a need to[/color]
            write[color=blue]
            > to and read from the registry. Config files will not be acceptable due to
            > their lack of security. The registry entry will have to do with the[/color]
            proper[color=blue]
            > registration and purchase of the program. I have the following code in my
            > program:
            >
            > Public Sub SetRegValue(ByV al ValueName As String, ByVal TestValue As
            > Boolean)
            >
            > Dim f As New RegistryPermiss ion(RegistryPer missionAccess.R ead Or
            > RegistryPermiss ionAccess.Write , "HKEY_LOCAL_MAC HINE\Software")
            >
            > Application.Com monAppDataRegis try.SetValue(Va lueName, TestValue)
            >
            > End Sub
            >
            > I receive the following error whenever I try to run the program:
            > Unhandled Exception: System.Security .SecurityExcept ion: Request for the
            > permission of type System.Security .Permissions.Re gistryPermissio n,[/color]
            mscorlib,[color=blue]
            > Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9[/color]
            failed.[color=blue]
            >
            > I am an administrator on my computer, so that can't be the problem. What[/color]
            am[color=blue]
            > I doing wrong?
            >
            >[/color]


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Unable to write to registry

              Vernon,
              [color=blue]
              >I am not trying to set hardware settings. I need program settings for
              > whomever is using the machine.[/color]

              What do you want to protect the machine or the software. The last has to be
              set in the software part from either the current or the default user. I gave
              you all the information in that link how to do that, what is your problem
              than.

              I have the idea that you did not even look for it, do you really want help.

              Cor


              Comment

              • Peter Huang [MSFT]

                #8
                RE: Unable to write to registry

                Hi

                Based on my test, the code below will create a subkey under HKLM/Software
                under the account with administrator rights.
                You may have a try.
                Sub Main()
                Dim test9999 As RegistryKey =
                Registry.LocalM achine.OpenSubK ey("SOFTWARE", True)
                Dim test8888 As RegistryKey = test9999.Create SubKey("MyTest" )
                test8888.SetVal ue("Test", "Hello World")
                End Sub

                Also the RegistryPermiss ion and FileIOPermissio n is used to control how
                code access to the registry/file(known as Code Access Security).

                For code access security, I think you may try to take a look at the link
                below as a start.
                Code Access Security
                http://msdn.microsoft.com/library/de...us/cpguide/htm
                l/cpconcodeaccess security.asp


                Best regards,

                Peter Huang
                Microsoft Online Partner Support

                Get Secure! - www.microsoft.com/security
                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                Working...