1

I´m trying to access a path in the registry inside Wow6432Node, to write a value inside it, but it is not working. I tried with different codes, but it´s still not working. I am running it as "Any CPU" on a x64 bit Windows 10. I suspect it has something to do with the difference on the registry between x86 and x64. Am I wrong?

if (rv3.Checked == true)
                {

                    string line2 = File.ReadLines(AppDomain.CurrentDomain.BaseDirectory + "simulators.txt").Skip(3).Take(1).First();
                    MessageBox.Show(line2);
                    if (System.IO.Directory.Exists(line2))
                        {
                        var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                        var Key = baseReg.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Microsoft Games\flight simulator\10.0\");
                        if (Key != null)
                        {
                            RegistryKey key2 = baseReg.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Microsoft Games\flight simulator\10.0\");



                            key2.SetValue("SetupPath", line2);
                            key2.Close();


                                }

                        }



                }

Kind regards!

CTABUYO
  • 662
  • 2
  • 7
  • 27
  • >but it´s still not working.< That's not enough information. – Lynn Crumbling Dec 21 '15 at 22:54
  • @LynnCrumbling, with that I mean that the value on the registry it´s still no changing. But I don´t get any exceptions though. – CTABUYO Dec 21 '15 at 22:55
  • Do you have "Prefer 32-bit" turned on in the Project Build settings? if so, then your process would be a 32-bit process. – Yacoub Massad Dec 21 '15 at 23:17
  • If your process is 32-bit, then you cannot access that node via "Wow6432Node". Instead you can access it without "Wow6432Node". the easiest way to check if your process is 32-bit is to see is the value of `IntPtr.Size` is 4. – Yacoub Massad Dec 21 '15 at 23:19
  • I have selected "Any CPU" @YacoubMassad – CTABUYO Dec 21 '15 at 23:20
  • Even if you selected "Any CPU", if the "Prefer 32-bit" option is selected, then your process would be 32-bit. – Yacoub Massad Dec 21 '15 at 23:21
  • You were right, it was running as 32-bit. But now I changed it to x64 and I get this exception: "System.UnauthorizedAccessException" when trying to write in the registry. – CTABUYO Dec 21 '15 at 23:26
  • Does your process have admin access to the machine? – Yacoub Massad Dec 21 '15 at 23:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98685/discussion-between-ctabuyo-and-yacoub-massad). – CTABUYO Dec 22 '15 at 11:35

1 Answers1

3

I ran into similar issues when working with the registry, and I suspect you're a victim of "virtualization".

On a machine with User Account Control (UAC) you aren't strictly denied permission to write to the registry, but the calls get virtualised. See if the values you are expected to see are turning up at HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software\.

The solution is to add an application manifest file to the solution, and set requestedExecutionLevel level="requireAdministrator", which means whenever you run the application on a machine with UAC it will ask "Are you sure?". You also need to ensure the project properties specify the manifest to use.

I then ran into a second problem, which is that when you're debugging through Visual Studio, it will run with VS execution level, not those specified in the manifest (see here). The easiest solution is to set VS to run as Administrator in the shortcut properties.

Community
  • 1
  • 1
Ian
  • 1,475
  • 2
  • 15
  • 31