How to get write permissions in C #

I try to write to the Windows registry at HKEY_CURRENT_USER \ Software \ appname, but I get a permission error all the time when I try to write the key, I added the following to my assembly:

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

but this did not solve the problem, is there anything else I should do?

+5
source share
5 answers

RegistryPermissionAttribute is part of Code Access Security aka CAS , a permission system that checks your permission inside the .NET environment, these permissions are determined by the security policy. There are 4 security policies :

  • Corporate policy for a family of machines that are part of an Active Directory installation.
  • Machine - .
  • - .
  • AppDomain - .

3 .NET Configuration, .

, , , RegistryPermissionAttribute .NET, .

System.Security.AccessControl, , , , , , .

+6

, , , , ? OpenSubKey(string) .

+7

, , .

0

. - .

, , , , exe. VS, , VS . VS "() .

0

This works for me. With the key already there and without it. Without 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 re-reading the question, it seems that the problem may be OS permissions.

-1
source

All Articles