0

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.

My code for registry creation

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

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());
}
rory.ap
  • 34,009
  • 10
  • 83
  • 174
Nitin Patil
  • 110
  • 10
  • Running as x86 or x64? On a 64-bit machine? – rory.ap Jun 09 '17 at 13:06
  • Looks like you're creating the key every time you run the program, so it would make sense that it is there even after deleting it manually and running again. – mjw Jun 09 '17 at 13:06
  • _What_ value is shown? You do a `CreateSubKey`, which creates the _key_ everytime anew. And `GetValue` does not throw an exception if the _value_ does not exist, but returns `null`, which is converted to `0` by `Convert.ToInt32()`. – René Vogt Jun 09 '17 at 13:08
  • If your program is running as 32-bit process on 64-bit OS, your program may actually read value from `HKLM\Software\WOW6432Node\TOPO` instead of `HKLM\Software\TOPO` because read from registry is redirected by WOW64 layer - see [How to view the system registry by using 64-bit versions of Windows](https://support.microsoft.com/en-us/help/305097/how-to-view-the-system-registry-by-using-64-bit-versions-of-windows) – Ňuf Jun 09 '17 at 13:50
  • @Ňuf Yes , You are correct its storing key into wow64, Please tell what changes i have to make in my code now ? – Nitin Patil Jun 10 '17 at 03:18

2 Answers2

2

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.

SOLUTION

Option 1

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.

Option 2

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.

Option 3

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

Ňuf
  • 6,027
  • 2
  • 23
  • 26
0

I had a similar problem and in my case I found there is one more kind of registry keys redirect for VSTO based Office Add-ins. If you use new Click-to-run installation of Office and save/read registry keys from within an Office Add-in it might be redirected to another location like: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\TOPO

Peter Liapin
  • 1,196
  • 9
  • 20