• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 951
  • Last Modified:

C# rename a registry folder

Hey Guys what is the easiest way to rename a folder in C# for example

to       HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft.oldfolder

much appretiated.
  • 2
1 Solution
Meir RivkinFull stack Software EngineerCommented:
unfortunately u can't simply rename registry key, u need to copy it, then delete the old one.
here code snippet to do so:
public bool RenameSubKey(RegistryKey parentKey, 
			string subKeyName, string newSubKeyName)
            CopyKey(parentKey, subKeyName, newSubKeyName);
            return true;

        /// <summary>
        /// Copy a registry key.  The parentKey must be writeable.
        /// </summary>
        /// <param name="parentKey"></param>
        /// <param name="keyNameToCopy"></param>
        /// <param name="newKeyName"></param>
        /// <returns></returns>
        public bool CopyKey(RegistryKey parentKey, 
			string keyNameToCopy, string newKeyName)
            //Create new key
            RegistryKey destinationKey = parentKey.CreateSubKey(newKeyName);

            //Open the sourceKey we are copying from
            RegistryKey sourceKey = parentKey.OpenSubKey(keyNameToCopy);

            RecurseCopyKey(sourceKey, destinationKey);

            return true;

        private void RecurseCopyKey(RegistryKey sourceKey, RegistryKey destinationKey)
            //copy all the values
            foreach (string valueName in sourceKey.GetValueNames())
                object objValue = sourceKey.GetValue(valueName);
                RegistryValueKind valKind = sourceKey.GetValueKind(valueName);
                destinationKey.SetValue(valueName, objValue, valKind);

            //For Each subKey 
            //Create a new subKey in destinationKey 
            //Call myself 
            foreach (string sourceSubKeyName in sourceKey.GetSubKeyNames())
                RegistryKey sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName);
                RegistryKey destSubKey = destinationKey.CreateSubKey(sourceSubKeyName);
                RecurseCopyKey(sourceSubKey, destSubKey);

Open in new window

kentrg11Author Commented:
thanks this is a little advanced for me and i will need to study up.
The above code is just a class i presume  and i would need to define these in another piece of code.
Thanks in advanced. Just trying to understand where to start
Meir RivkinFull stack Software EngineerCommented:
u don't have to put this code in a separate class, but its recommended just to make the code cleaner.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now