Unable to write to registry

V

Vernon Peppers

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(ByVal ValueName As String, ByVal TestValue As
Boolean)

Dim f As New RegistryPermission(RegistryPermissionAccess.Read Or
RegistryPermissionAccess.Write, "HKEY_LOCAL_MACHINE\Software")

Application.CommonAppDataRegistry.SetValue(ValueName, TestValue)

End Sub

I receive the following error whenever I try to run the program:
Unhandled Exception: System.Security.SecurityException: Request for the
permission of type System.Security.Permissions.RegistryPermission, mscorlib,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.

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

Guest

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

ex:

SaveSetting("Program", "Section", "Key", "Setting")

Dim temp as string
temp = GetSetting("Program", "Section", "Key")
 
P

Peter Huang [MSFT]

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.LocalMachine.OpenSubKey("SOFTWARE", True)
Dim test8888 As RegistryKey = test9999.CreateSubKey("MyTest")
test8888.SetValue("Test", "Hello World")
End Sub

Also the RegistryPermission and FileIOPermission 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/default.asp?url=/library/en-us/cpguide/htm
l/cpconcodeaccesssecurity.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top