gpt4 book ai didi

c# - 使用 C# 删除注册表项

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:15 25 4
gpt4 key购买 nike

我正在尝试删除这样的注册表项:

RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", true);

oRegistryKey.DeleteSubKeyTree(".");

但这给了我一个异常(exception):

Cannot delete a subkey tree because the subkey does not exist

如果我将 DeleteSubKeyTree 更改为 DeleteSubKey,我会收到一个不同的异常:

Registry key has subkeys and recursive removes are not supported by this method

最佳答案

this answer 中概述的方法不必要的复杂因为DeleteSubKeyTree是递归的。来自其在 MSDN 上的文档:

Deletes a subkey and any child subkeys recursively.

因此,如果您的目标是删除用户的 FileExts 键,请执行以下操作:

string explorerKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Explorer";

using (RegistryKey explorerKey =
Registry.CurrentUser.OpenSubKey(explorerKeyPath, writable: true))
{
if (explorerKey != null)
{
explorerKey.DeleteSubKeyTree("FileExts");
}
}

但是,您确定您真的想要删除用户的 FileExts 键吗?我相信大多数人会认为这样做是不合理的破坏性和鲁莽行为。更常见的情况是从 FileExts 键中删除单个文件扩展键(例如 .hdr)。

最后,请注意 DeleteSubKeyTree 已过载。这是 second version 的签名这种方法:

public void DeleteSubKeyTree(
string subkey,
bool throwOnMissingSubKey
)

在此版本中,如果 subkey 不存在且 throwOnMissingSubKey 为 false,DeleteSubKeyTree 将简单地返回而不对注册表进行任何更改。

关于c# - 使用 C# 删除注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com