注册表项已删除但仍从注册表 c# 中获取值 [英] Registry key deleted but still getting value from registry c#

查看:29
本文介绍了注册表项已删除但仍从注册表 c# 中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 Windows 应用程序.当我通过代码创建注册表值时,它运行良好,但从注册表中删除后(我正在使用 cmd 中的 regedit 删除该注册表).当我从 Visual Studio 调试时,仍然在我的代码中显示该值.

I created windows application. When i am creating registry value through code its working well but after deleting from registry (i am deleting that registry using regedit from cmd). Still showing that value in my code when i am debugging from visual studio.

我的注册表创建代码

RegistryKey key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\TOPO");
if (key != null)
{
   //key.SetValue("interval", "5000");
   key.SetValue("Topos", 1, RegistryValueKind.DWord);
   key.Close();
}

然后我正在检查 Program.cs 中的值

then i am checking value from Program.cs

string strval = string.Empty;
RegistryKey key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\TOPO");
if (key != null)
{
   //key.SetValue("interval", "5000");
   bb = Convert.ToInt32(key.GetValue("Topos"));
   key.Close();
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

if (bb == 1)
{ 
    Application.Run(new frm_mdi());
}
else
{
    Application.Run(new frm_activation());
}

推荐答案

在 Windows XP 和更新版本中,某些注册表项在 32 位和 64 位进程之间重定向、反映或共享.根据 这篇文章.有关重定向的更多信息此处.因此,当您使用 Regedit(64 位)从注册表中删除密钥时,该密钥的 32 位版本仍保留在注册表中.

In Windows XP and newer, certain registry keys are either redirected, reflected or shared between 32-bit and 64-bit processes. Particularly HKLM\Software\TOPO should be redirected to HKLM\Software\WOW6432Node\TOPO, according to this article. More on redirection here. Therefore when you delete key from registry using Regedit (which is 64-bit), 32-bit version of the key still remains in registry.

选项 1

如果可能,让您的应用程序在 64 位 Windows 上作为 64 位进程运行.假设您使用的是 Visual Studio 并且您的应用程序面向任何 CPU 解决方案平台,您可以通过右键单击项目 -> 属性 -> Build 选项卡并取消选中所有所需配置的首选 32 位"复选框.

If possible, let your application run as 64-bit process on 64-bit Windows. Assuming you are using Visual Studio and your application targets Any CPU solution platform, you can do this by right-click on project -> Properties -> Build tab and unchecking "Prefer 32-bit" checkbox for all desired configurations.

选项 2

修改您的程序,以访问 64 位版本的注册表,即使它作为 32 位进程运行.你可以这样做:

Modify your program, to access 64-bit version of registry even if it is running as 32-bit process. You can do this with something like this:

var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    .OpenSubKey(@"SOFTWARE\TOPO");

如果您需要更多详细信息,请参阅这个答案.

See this answer if you need more details.

选项 3

使用regedit删除/修改/创建注册表时,只需编辑HKLM\Software\WOW6432Node\TOPO键而不是HKLM\Software\TOPO.

When deleting/modifying/creating registry using regedit, simply edit HKLM\Software\WOW6432Node\TOPO key instead of HKLM\Software\TOPO.

这篇关于注册表项已删除但仍从注册表 c# 中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆