C# 使用 Registry.LocalMachine.OpenSubKey 时出现安全异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/678873/
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 13:07:41  来源:igfitidea点击:

SecurityException when using Registry.LocalMachine.OpenSubKey

c#securitywindows-vistaregistry

提问by Craig Shearer

I'm developing an application that needs to write to the registry. It works fine on XP, but when I run it on Vista, from Visual Studio, I get a security exception in:

我正在开发一个需要写入注册表的应用程序。它在 XP 上运行良好,但是当我在 Vista 上从 Visual Studio 运行它时,出现以下安全异常:

Registry.LocalMachine.OpenSubKey("SOFTWARE", true);

Registry.LocalMachine.OpenSubKey("SOFTWARE", true);

I'm trying to write a new key into that branch of the registry.

我正在尝试将新密钥写入注册表的该分支。

What's the right way to do this, firstly so that I can run my application from VS on Vista, and secondly so that my users don't run into problems running on Vista.

这样做的正确方法是什么,首先是为了我可以在 Vista 上从 VS 运行我的应用程序,其次是为了让我的用户不会在 Vista 上运行时遇到问题。

Thanks...

谢谢...

采纳答案by Bevan

On both XP and Vista you need Admin rights to write a new key under LocalMachine.

在 XP 和 Vista 上,您都需要管理员权限才能在 LocalMachine 下编写新密钥。

You'll be finding this works on XP and fails on Vista due to different account defaults.

您会发现这在 XP 上有效,而在 Vista 上由于不同的帐户默认设置而失败。

The quick and dirty solution is to ensure your application runs with Admin rights in both cases, though in on Vista this tends to be frowned upon.

快速而肮脏的解决方案是确保您的应用程序在这两种情况下都以管理员权限运行,尽管在 Vista 中这往往不受欢迎。

The better solution would be to redesign things slightly - can the new sub key be written by your installer (which runs with Admin rights), or could you store your information somewhere else?

更好的解决方案是稍微重新设计一下 - 新的子密钥可以由您的安装程序编写(以管理员权限运行),还是可以将您的信息存储在其他地方?

回答by Michael

Standard users, and admin's running with UAC on Vista, do not have permission to write the the local machine registry key. This would fail on XP too if you ran as standard user.

在 Vista 上使用 UAC 运行的标准用户和管理员无权写入本地计算机注册表项。如果您以标准用户身份运行,这也会在 XP 上失败。

Your options are:

您的选择是:

  • Use Registry.CurrentUser instead, if the setting is per-user.
  • Run your app as administrator
  • Loosen the ACL on the key so anyone can write - which is definitely not recommended, since any malware on the box can toast the key.
  • 如果设置是针对每个用户的,请改用 Registry.CurrentUser。
  • 以管理员身份运行您的应用
  • 松开密钥上的 ACL,以便任何人都可以写入 - 绝对不建议这样做,因为盒子上的任何恶意软件都可以烘烤密钥。

回答by Ray

You can only write to that key if you're running as administrator. So you'll need to run VS as administrator and your users will need to run the application as administrator.

如果您以管理员身份运行,则只能写入该密钥。因此,您需要以管理员身份运行 VS,而您的用户需要以管理员身份运行应用程序。

My suggestion would be to see if you really need to write to LocalMachine. You can write to CurrentUser without admin rights.

我的建议是看看您是否真的需要写入 LocalMachine。您可以在没有管理员权限的情况下写入 CurrentUser。

回答by Henk Holterman

I assume it "works under XP" because everybody runs it as Admin under XP?

我假设它“在 XP 下工作”,因为每个人都在 XP 下以管理员身份运行它?

  • You will could try to circumvent (create the key during Setup or use a key under CurrrentUser or something).

  • You could grant the privilege to your Application during Setup. I'm afraid I'm a little short on details on how to do that.

  • 您可以尝试规避(在安装过程中创建密钥或使用 CurrrentUser 下的密钥或其他东西)。

  • 您可以在安装过程中授予您的应用程序权限。恐怕我对如何做到这一点的细节有点缺乏。

回答by AVEbrahimi

Use Registry.CurrentUser

使用 Registry.CurrentUser