我试图删除这样的注册表项:
RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", true);
oRegistryKey.DeleteSubKeyTree(".");
但这给了我一个例外:
无法删除子项树,因为该子项不存在
如果将
DeleteSubKeyTree
更改为DeleteSubKey
,则会收到另一个异常:注册表项具有子项,并且此方法不支持递归删除
最佳答案
尝试这个:
string str = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts";
string[] strSplit = strLocal.Split('\\');
using (RegistryKey oRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts", true))
{
RegistryKey hdr = oRegistryKey.OpenSubKey(strSplit[strSplit.Length-2], true);
foreach (String key in hdr.GetSubKeyNames())
hdr.DeleteSubKey(key);
hdr.Close();
oRegistryKey.DeleteSubKeyTree(strSplit[strSplit.Length - 2]);
}
还要检查:Registry in .NET: DeleteSubKeyTree says the subkey does not exists, but hey, it does!