Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C# 3.5

How to delete 64 bit Registry keys/values (.NET 3.5)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
20 May 2013CPOL3 min read 39K   602   8  
How to access a 64bit registry key from 32bit built application in .NET 3.5 and lower
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace X64RegOps
{
    public partial class Form1 : Form
    {
        enum BaseKeys
        {
            HKEY_LOCAL_MACHINE,
            HKEY_CURRENT_USER,
            HKEY_CLASSES_ROOT,
            HKEY_USERS,
            HKEY_CURRENT_CONFIG
        }

        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("Subkey value is empty...");
                return;
            }

            UIntPtr basekey = GetX64BaseKey(comboBox1.Text);
            string subkey = textBox1.Text;

            DeleteKey(basekey, subkey);
        }

        protected UIntPtr GetX64BaseKey(string basekeystr)
        {
            UIntPtr baseKey = UIntPtr.Zero;
            if (basekeystr.Equals(BaseKeys.HKEY_LOCAL_MACHINE.ToString()))
                baseKey = RegistryInterop.HKEY_LOCAL_MACHINE;
            else if (basekeystr.Equals(BaseKeys.HKEY_CURRENT_USER.ToString()))
                baseKey = RegistryInterop.HKEY_CURRENT_USER;
            else if (basekeystr.Equals(BaseKeys.HKEY_CLASSES_ROOT.ToString()))
                baseKey = RegistryInterop.HKEY_CLASSES_ROOT;
            else if (basekeystr.Equals(BaseKeys.HKEY_USERS.ToString()))
                baseKey = RegistryInterop.HKEY_USERS;
            else if (basekeystr.Equals(BaseKeys.HKEY_CURRENT_CONFIG.ToString()))
                baseKey = RegistryInterop.HKEY_CURRENT_CONFIG;

            return baseKey;
        }

        // Delete a subkey (a registry tree) from HKEY_LOCAL_MACHINE
        private void DeleteKey(UIntPtr basekey, string subkey)
        {
            // Open HKEY_LOCAL_MACHINE
            using (X64RegistryKey baseRegkey = new X64RegistryKey(basekey))
            {
                UIntPtr key = UIntPtr.Zero;
                if (X64RegistryKey.SUCCESS == baseRegkey.Open(subkey, out key)) // open the subkey under HKEY_LOCAL_MACHINE\subkey to delete it
                {
                    using (X64RegistryKey regkey = new X64RegistryKey(key))
                    {
                        regkey.DisableReflection();    // call this method to disable registry reflection, so that the 64bit registry can be deleted
                        regkey.RegDeleteTree(subkey);
                    }
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
India India
Have great experience in design, architecture & delivery of services built using Microservices architecture, Enterprise Server applications using scaled RDBMS and NoSQL databases. Have used a variety open-source softwares to build solutions like - Apache Ignite, REDIS Distributed Cache, Apache Kafka as Service BUS, Elasticsearch (ELK stack for log aggregation, queries and visualisation), MongoDB and MySQL databases. In the past, I have developed multiple applications & services for Windows & Android platforms as well.

Interested in high availability and scalability articles.

Comments and Discussions