Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

Copy and Rename Registry Keys

Rate me:
Please Sign up or sign in to vote.
3.42/5 (19 votes)
11 Nov 2006CPOL2 min read 78.2K   1.1K   12  
A utility that will copy and rename registry keys
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

// RenameRegistryKey � Copyright 2006 Active Computing
namespace RenameRegistryKey
{
    public partial class Test : Form
    {
        const string _testRootKeyName = "copyRegKeyTest";
        const string _testRootKeyNameRenamed = "copyRegKeyTestRenamed";

        public Test()
        {
            InitializeComponent();
        }

        private void cmdRunTest_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("We are going to add keys to your registry and then rename them.  Do you want to continue?", "RenameRegistryKey Test", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                CreateRegistryKeys();
                MessageBox.Show("If you open your registry editor (Start->Run type in regedit) You will see we added a new key called:" + Environment.NewLine + "copyRegKeyTest" + Environment.NewLine + "located under HKEY_CURRENT_USER.  Next we will rename it.");


                DoTheRename();
                MessageBox.Show("If you refresh you registry editor (press F5) you will see the registry key has now been renamed to: copyRegKeyTestRenamed");
            }
            else
            {
                MessageBox.Show("You decided not to run the test.  No changes made to your registry.");
            }
        }

        /// <summary>
        /// Sets up the test by creating registry keys that we will rename.
        /// </summary>
        private void CreateRegistryKeys()
        {
            RegistryKey currentUserRootKey = Registry.CurrentUser;
            RegistryKey testRootKey = currentUserRootKey.OpenSubKey(_testRootKeyName, true);
            if (testRootKey == null)
            {
                //test key doesn't exist so create it.
                testRootKey = currentUserRootKey.CreateSubKey(_testRootKeyName);
            }

            //Add some values
            string valName = "Create Date";
            object testValue = System.DateTime.Now;
            RegistryValueKind valKind = RegistryValueKind.String;

            testRootKey.SetValue(valName, testValue, valKind);

            valName = "Name";
            testValue = "Your Name";
            valKind = RegistryValueKind.ExpandString;

            testRootKey.SetValue(valName, testValue, valKind);



            //Add a sub key
            RegistryKey subKey = testRootKey.CreateSubKey("MyFirstSubKey");

            //Add more values to our sub key
            valName = "Pet";
            testValue = "Ralph the dog";
            valKind = RegistryValueKind.Unknown;

            subKey.SetValue(valName, testValue, valKind);
        }

        public void DoTheRename()
        {
            RegistryUtilities ru = new RegistryUtilities();
            ru.RenameSubKey(Registry.CurrentUser, _testRootKeyName, _testRootKeyNameRenamed);
        }
    }
}

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions