这个问题here和here可能有重复之处,但尚未得到充分回答。
我很乐意重命名一个用户文件夹,并且不得不更改注册表中的“一些”键。这已经完成了,但是我很好奇一个人是否可以自动完成这个过程。为此,我试图像这样遍历注册表树:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
namespace RegistryReplacer
{
class Program
{
private static void printKey(RegistryKey key)
{
Console.WriteLine("{0} has {1} sub keys", key.Name, key.SubKeyCount);
}
private static void traverseKey(RegistryKey key)
{
using (key)
{
printKey(key);
string[] subKeyNames = key.GetSubKeyNames();
foreach(string subKeyName in subKeyNames)
{
RegistryKey subKey = key.OpenSubKey(subKeyName);
traverseKey(subKey);
}
}
}
static void Main(string[] args)
{
List<RegistryKey> rootKeys = new List<RegistryKey> {
Registry.LocalMachine,
Registry.CurrentUser,
Registry.ClassesRoot,
Registry.CurrentConfig,
Registry.Users
};
foreach (RegistryKey rootKey in rootKeys)
{
traverseKey(rootKey);
}
}
}
}我可以遍历树的根(LocalMachine等),但是当对某些键(不是全部)调用OpenSubKey时,我会遇到
System.Security.SecurityException:“请求的注册表访问是不允许的。”
我使用管理员权限运行应用程序(使用rightclick+“作为管理员运行”或使用this),但这没有帮助。这是在我的个人计算机(64位Windows 10 Pro,版本2004),我是管理员,并有足够的权利通过regedit更改注册表。
我做错了什么?
发布于 2020-08-01 04:12:40
这个问题与注册表项所有权有关--它很可能具有自定义权限,并且由系统、TrustedInstaller或其他帐户拥有。
下面是一个非常好的答案,它深入地解释了问题:https://superuser.com/a/493121。
所以,你做的一切都是正确的,它并不像预期的那样工作。“这不是一个bug,这是一个特性”(c)
https://stackoverflow.com/questions/63081156
复制相似问题