如何在 C# 中获取注册表写入权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/721145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:27:11  来源:igfitidea点击:

How to get registry write permissions in C#

c#

提问by UnkwnTech

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:

我正在尝试写入 HKEY_CURRENT_USER\Software\appname 处的 Windows 注册表,但是当我尝试写入密钥时,我不断收到权限错误,我已将以下内容添加到我的程序集中:

[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?

但这并没有解决问题,我还应该做些什么吗?

采纳答案by Davy Landman

The RegistryPermissionAttributeis 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:

RegistryPermissionAttribute是部分代码访问安全又称CAS,这是检查你有内部的.NET framework的权限,这些权限由安全策略定义的权限系统。有4个安全策略

  • 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.
  • 企业 - 属于 Active Directory 安装一部分的一系列机器的策略。
  • 机器 - 当前机器的策略。
  • 用户 - 登录用户的策略。
  • AppDomain - 执行应用程序域的策略。

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

前 3 个在 .NET 配置工具的配置屏幕中配置,最后一个在运行时配置。

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

我首先解释这一点的原因是因为 RegistryPermissionAttribute 只检查您的 .NET 权限,它不检查操作系统权限。

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

您可以使用System.Security.AccessControl检查操作系统权限,但要获得权限,您可能需要提升或模拟。

回答by Jon Skeet

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.

我不认为它像您打开密钥而不指定您想要写访问权限那么简单?该OpenSubKey(string)方法仅提供只读访问权限。

回答by pero

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.

编辑:重新阅读问题后,似乎问题可能出在操作系统权限上。

回答by RuudKok

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

确保应用程序使用具有访问注册表的足够权限的用户帐户运行。

回答by Doc

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.

我认为可能需要的一件事是,如果您从 exe 运行,则需要以管理员身份运行。如果您从 VS 运行,则需要确保 VS 以管理员身份运行。如果是,VS 将在窗口标题中显示“(管理员)”。