We know that the target platform is 32-bit.ProgramRunning on a 64-bit system, the system automatically redirects to the corresponding items of the win32node node when accessing some registries. However, developers who have installed programs may encounter the issue of "Removing redirection is required", that is, they directly access the registry of the 64-bit program.
There are many winapi methods on the Internet, and it is a little complicated to disable registry redirection. (It is a little easier to disable the file system redirection. You can use it after you move it. I do not understand the redirection of the disabled registry .)
The methods provided here do not need to disable redirection or use winapi, which makes it much easier to operate. The details are as follows:
The first method is to obtain the root node handle, and the constant is fixed.
Static Intptr gethivehandle (registryhive hive) {intptr preexistinghandle = Intptr. Zero; intptr hkey_classes_root = New Intptr (- 2147483648 ); Intptr HKEY_CURRENT_USER =New Intptr (- 2147483647 ); Intptr HKEY_LOCAL_MACHINE = New Intptr (- 2147483646 ); Intptr HKEY_USERS = New Intptr (- 2147483645 ); Intptr hkey_cece_data = New Intptr (- 2147483644 ); Intptr hkey_current_config =New Intptr (- 2147483643 ); Intptr hkey_dyn_data = New Intptr (- 2147483642 ); Switch (Hive ){ Case Registryhive. classesroot: preexistinghandle = hkey_classes_root; Break ; Case Registryhive. currentuser: preexistinghandle = HKEY_CURRENT_USER;Break ; Case Registryhive. localmachine: preexistinghandle = HKEY_LOCAL_MACHINE; Break ; Case Registryhive. Users: preexistinghandle = HKEY_USERS; Break ; Case Registryhive. performancedata: preexistinghandle = hkey_performance_data; Break ; Case Registryhive. currentconfig: preexistinghandle = hkey_current_config; Break ; Case Registryhive. dyndata: preexistinghandle = hkey_dyn_data; Break ;} Return Preexistinghandle ;} /// <Summary> /// For 32-bit applications to access the 64-bit Registry /// </Summary> /// <Param name = "hive"> Root-level name </Param> /// <Param name = "keyname"> Excluding root-level names </Param> /// <Param name = "valuename"> Item Name </Param> /// <Param name = "View"> Registry View </Param> /// <Returns> Value </Returns> Static Object Getvaluewithregview (registryhive hive, String Keyname, String Valuename, registryview view) {saferegistryhandle handle = New Saferegistryhandle (gethivehandle (hive ), True ); // Obtain the Security handle of the root node. Registrykey subkey = Registrykey. fromhandle (handle, view). opensubkey (keyname ); // Obtain the access key Registrykey key = Registrykey. fromhandle (subkey. Handle, view ); // Obtain the access key based on the key handle and view. Return Key. getvalue (valuename ); // Obtains the value of a specified key. } /// <Summary> /// Set the 64-bit registry for 32-bit programs /// </Summary> /// <Param name = "hive"> Root-level name </Param> /// <Param name = "keyname"> Excluding root-level names </Param> /// <Param name = "valuename"> Item Name </Param> /// <Param name = "value"> Value </Param> /// <Param name = "Kind"> Value Type </Param> /// <Param name = "View"> Registry View </Param> Static Void Setvaluewithregview (registryhive hive,String Keyname, String Valuename, Object Value, registryvaluekind kind, registryview view) {saferegistryhandle handle = New Saferegistryhandle (gethivehandle (hive ), True ); Registrykey subkey = Registrykey. fromhandle (handle, view). opensubkey (keyname, True ); // The write permission is required. True is the key. 0227 update Registrykey key = Registrykey. fromhandle (subkey. Handle, view); key. setvalue (valuename, value, kind );}
To use these methods, you need to introduce the three namespaces Microsoft. win32.safehandles; Microsoft. Win32; system. The method only provides the trunk and does not handle exceptions. We hope you will discuss your actual usage in a timely manner. Please point out the shortcomings in a timely manner.
For winapi methods, visit http://home.cnblogs.com/u/xuguilin/ArticleAnd thanks to xuguilin for its guidance. According to reports, the DOTNET framework may require version 4.0 or later.