【发布时间】:2011-09-08 09:29:13
【问题描述】:
我创建了以下注册表项(通过 regedit 复制):
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test
我现在想删除此注册表项,所以...我一直在使用以下代码,但遇到了一个小错误。
RegistryKey regKey;
string regPath_Key = @"Software\Microsoft\Windows\CurrentVersion\test";
regKey = Registry.CurrentUser.OpenSubKey(regPath_Key, true);
if(regKey != null) // Always returns null, even though the key does exist.
{
Registry.CurrentUser.DeleteSubKey(regPath_Key, true);
}
我遇到的问题是 if(regKey != null) always 行返回 null!我已经回去检查该密钥是否确实存在多次 - 但仍然是相同的结果。我会假设我的代码在某处有问题?
【问题讨论】:
-
我认为你的路径应该是
\Software\Microsoft\Windows\CurrentVersion\test,因为你已经从Registry.CurrentUser进入HKCU,所以它正在寻找HKEY_CURRENT_USER\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test -
@vcsjones:你为什么不把它作为答案发布(没有第一个反斜杠)?
-
天哪...我复制的不正确-你说得对...
-
@Mat 无法检验我的理论,我也不确定能否给出答案。如果有帮助,那就太棒了。