C# Access 64 bit Registry
I was wondering if it was possible to access the following registry key in C# on a 64 bit pc.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
When accessing on a 开发者_Go百科32bit pc it works fine but on 64 it re - directs it to the native 64bit path of HKLM\SOFTWARE\Wow6432Node which has different keys. I have looked through various articles on this but can't really find a definite answer on how to access the 32bit key in a 64bit pc in C#. Thanks.
If you are targetting the .Net Framework version 4.0 or above then you can do this by passing the RegistryView.Registry32 value when opening the desired registry key.
If you are targetting a previous version of the .Net Framework then you need to use P/Invoke to call RegOpenKeyEx directly yourself allowing you to pass the KEY_WOW64_32KEY flag.
There is a guide here that goes into more detail:
- How to read the 64 bit registry from a 32 bit application or vice versa
Compile your application in x64, and all should be well. In Visual Studio 2010, you'd do that by changing the setting under Project Properties > Build
For VS Express users:
In VC# Express, this property is missing, but you can still create an x86 configuration if you know where to look.
It looks like a long list of steps, but once you know where these things are it's a lot easier. Anyone who only has VC# Express will probably find this useful. Once you know about Configuration Manager, it'll be much more intuitive the next time.
- In VC# Express, go to Tools -> Options.
- In the bottom-left corner of the Options dialog, check the box that says, "Show all settings".
- In the tree-view on the left hand side, select "Projects and Solutions".
- In the options on the right, check the box that says, "Show advanced build configuraions."
- Click OK.
- Go to Build -> Configuration Manager...
- In the Platform column next to your project, click the combobox and select "".
- In the "New platform" setting, choose "x64".
- Click OK.
- Click Close.
- There, now you have an x64 configuration! Easy as pie! :-)
Project + Properties, Build tab, Platform target = Any CPU. Accessing the 64-bit registry from a 32-bit app requires .NET 4 and the new RegistryKey.OpenBaseKey() method with the RegistryView.Registry64 option.
This only lets you read the key, writing the key values requires UAC elevation. You can write to HKCU without elevation and without being subjected to registry redirection.
You have that backwards, the Wow6432Node is for 32-bit applications. So if your application si 32-bit (x86) then you would automatically be redirected to that "node".
You can use the FromHandle method in .NET 4 to specify which view to use, but it's use is not very obvious and there can be problems.
This answer addresses this question using the Win32 API, which you can leverage in C# as well.
Its suppose to redirect. You need to detect this redirection and read the WoW6432Node key instead. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run is the 64-bit registry which can only be accessed by 64-bit applications.
Obviously you shoudl write code to support both.
精彩评论