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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.