c#读取注册表Windows 8 [英] c# read registry windows 8

查看:63
本文介绍了c#读取注册表Windows 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的程序,它需要使用以下方法访问注册表:

I have an existing program, which demands for registry access using:

RegistryPermission permission = new RegistryPermission(RegistryPermissionAccess.AllAccess, strCheckPermission);
permission.Demand();

这适用于 Windows XP 到 Windows 7,以及 Windows Server 2003 和 2008.但是,当我在 Windows 8 上运行它时,当我要求此权限集时,我仍然无法创建子密钥

This works fine for Windows XP till Windows 7, and Windows Server 2003 and 2008. However, when I run this on Windows 8, when I demand this permission set, I still can't create a SubKey

Registry.LocalMachine.OpenSubKey("Software", true).CreateSubKey("myCompany")

有人知道这里出了什么问题吗?

Does anyone know what's going wrong in here?

更新:异常的堆栈跟踪:

Update: a stacktrace of the exception:

System.UnauthorizedAccessException: Cannot write to the registry key.
   at Microsoft.Win32.RegistryKey.EnsureWriteable()
   at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity)

推荐答案

您将代码访问安全权限与 Windows 权限混为一谈.CAS 不做任何事情来让您访问注册表项,它只是验证权限没有被禁用.大多数 .NET 应用程序在完全信任的情况下运行,但这种情况很少发生.

You are mixing up Code Access Security permissions with Windows rights. CAS does not do anything to give you access to registry keys, it merely verifies that the permission wasn't disabled. Which it rarely is, most .NET apps run in full trust.

这是一个Window访问权限问题,您的用户帐户根本没有写入密钥的权限.这并不罕见,HKLM 密钥只能用 UAC 提升写入.你需要嵌入一个清单来要求提升,检查 此答案 了解详情.

This is a Window access right problem, your user account simply doesn't have the permission to write the key. Which is not unusual, HKLM keys can only ever be written with UAC elevation. You'll need to embed a manifest to ask for elevation, check this answer for details.

另一个 Windows 8 特定细节是 WinRT,Windows 应用商店"应用将永远无法写入此类密钥,没有解决方法.

A further Windows 8 specific detail is WinRT, a "Windows Store" app will never be able to write such keys, no workaround for that.

这篇关于c#读取注册表Windows 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆