简体   繁体   English

删除子项错误 (C#)

[英]Delete Subkey error (C#)

I have created the following registry key (copied through regedit):我创建了以下注册表项(通过 regedit 复制):

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\test

I would like to now delete this registry key, and so... I have been using the following code and am running into a small error.我现在想删除此注册表项,所以...我一直在使用以下代码,但遇到了一个小错误。

RegistryKey regKey;注册表键 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);
    }

The issue I am having is that the line if(regKey != null) always returns null.我遇到的问题是if(regKey != null)总是返回 null。 I have gone back and checked that the key does in fact exist multiple times - but still the same result?我回过头来检查该密钥是否确实存在多次 - 但结果仍然相同? I am going to assume my code has issues somewhere?我将假设我的代码在某处有问题?

Could it be that you are on a 64 bit machine and your project is set to x86 architecture?会不会是您在 64 位机器上,并且您的项目设置为 x86 架构? in that case, verify that the key you state exists under HKCU\Software\Wow6432Node... as every path is redirected to this 32 bit process registry...在这种情况下,请验证您 state 的密钥是否存在于 HKCU\Software\Wow6432Node... 下,因为每个路径都被重定向到此 32 位进程注册表...

You should not include HKEY_CURRENT_USER in the string you pass to Registry.CurrentUser.OpenSubKey() .您不应在传递给Registry.CurrentUser.OpenSubKey()的字符串中包含HKEY_CURRENT_USER Instead use而是使用

string regPath_Key = @"Software\Microsoft\Windows\CurrentVersion\test";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM