Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get registry write permissions in C#

Tags:

c#

I'm trying to write to the windows registry at HKEY_CURRENT_USER\Software\appname however I keep getting a permissions error when I attempt to write to the key, I have added the following to my assembly:

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, Write = @"HKEY_CURRENT_USER\\Software")]

but this has not resolved the issue, is there something else that I should be doing?

like image 791
UnkwnTech Avatar asked Apr 06 '09 12:04

UnkwnTech


2 Answers

I don't suppose it's something as simple as you having opened the key without specifying that you want write access? The OpenSubKey(string) method only gives read-only access.

like image 95
Jon Skeet Avatar answered Oct 01 '22 01:10

Jon Skeet


The RegistryPermissionAttribute is part of the Code Access Security aka CAS, this is a permission system which checks the permission you have inside of the .NET framework, these permissions are defined by the security policy. There are 4 security policies:

  • Enterprise - policy for a family of machines that are part of an Active Directory installation.
  • Machine - policy for the current machine.
  • User - policy for the logged on user.
  • AppDomain - policy for the executing application domain.

The first 3 are configured in the configuration screen in the .NET Configuration tool, and the last is configured at runtime.

The reason why I explain this first is because the RegistryPermissionAttribute only checks your .NET permissions, it does not check the Operating System permissions.

You could use the System.Security.AccessControl to check the operating system permissions, but to get the permissions you'll probably need to either elevate or impersonate.

like image 32
Davy Landman Avatar answered Oct 01 '22 00:10

Davy Landman