UnkwnTech
UnkwnTech

Reputation: 90831

How to get registry write permissions in 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?

Upvotes: 7

Views: 11448

Answers (5)

Doc
Doc

Reputation: 708

I don't see an answer or resolution here. I found this question when searching for something else.

The one thing I think that may be needed is that you need to be running as administrator if you're running from the exe. If you're running from VS you'll need to make sure that VS is running as administrator. VS will show "(Administrator) in the window title if it is.

Upvotes: 0

Davy Landman
Davy Landman

Reputation: 15439

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.

Upvotes: 7

pero
pero

Reputation: 4259

This works for me. With key already there and without it. Without any special assembly attributes.

using System;
using Microsoft.Win32;

namespace WriteToRegistry {
  class Program {
    static void Main(string[] args) {
      const string csRootKey = @"Software\MyCompany\Test";

      using (RegistryKey loRegistryKey = Registry.CurrentUser.CreateSubKey(csRootKey)) {
        if (loRegistryKey == null)
          throw new InvalidOperationException("Could not create sub key " + csRootKey);

        loRegistryKey.SetValue("CurrentTime", DateTime.Now.ToString(), RegistryValueKind.String);
      }
    }
  }
}

EDIT: After rereading the question it seems that the problem could be OS permissions.

Upvotes: -1

RuudKok
RuudKok

Reputation: 5302

Make sure the app runs using the user-account that has enough rights to access the registry.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 8

Related Questions