Let's assume we write the program which interacts with 64-bit Windows OS. Here is the code which writes into HKEY_LOCAL_MACHINE registry of it.
- var softwareSubKey = Registry.LocalMachine.OpenSubKey
- ("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
- softwareSubKey.CreateSubKey("MySoftware");
Registry redirection consistently works with the same location, so code below works consistently regardless of platform chosen (although operates with different physical path)
- private static void Write()
- {
- var softwareSubKey = Registry.LocalMachine.OpenSubKey
- ("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
- softwareSubKey.CreateSubKey("MySoftware");
- }
- private static void Delete()
- {
- var softwareSubKey = Registry.LocalMachine.OpenSubKey
- ("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
- try
- {
- softwareSubKey.DeleteSubKeyTree("MySoftware", true);
- }
- catch (ArgumentException)
- {
- Console.WriteLine("Gotcha!");
- Console.ReadKey();
- }
- }
- static void Main(string[] args)
- {
- Write();
- Delete();
- }
All entries for which registry redirection is applied are described in the documentation.
To override registry redirection use RegistryKey.OpenBaseKey overload which accepts RegistryView as a parameter. Check the code below
- var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
- var softwareSubKey = localMachine.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
- softwareSubKey.CreateSubKey("MySoftware");

Comments
Join the conversation! Your thoughts help the community grow.