Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello i read this RegRenameKey – Hidden registry API | Pavel's Blog[^]

I am trying to rename the key doing this:

Code:
C#
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646);
        [DllImport("advapi32")]
        public static extern int RegRenameKey(SafeRegistryHandle hKey, [MarshalAs(UnmanagedType.LPWStr)] string oldname,
        [MarshalAs(UnmanagedType.LPWStr)] string newname);
        static void Main(string[] args)
        {
            
            SafeRegistryHandle hKey = null;
            hKey = new SafeRegistryHandle(HKEY_LOCAL_MACHINE,true);
            RegRenameKey(hKey, "SOFTWARE\\Company", "SOFTWARE\\Compa");
            Console.ReadLine();
            
        }
    }
}


What I have tried:

i was trying to rename the key the program compile fine but at the moment i dind get rename the key help pls..
Posted
Updated 25-Mar-17 19:54pm
v2
Comments
Bryian Tan 25-Mar-17 18:19pm    
Have you try run as Administrator? By the way, why you want to write code to mess with the Registry anyway?
Omar Alami 25-Mar-17 19:43pm    
I didnt read the key maybe for this..

1 solution

There are a couple of problems here:
1) You are using undocumented functions. This is dangerous as there is absolutely no guarantee that they will:
1.1) Work.
1.2) Work on an operating system other than that it was written for.
1.3) Exist on later OSes.

2) Access to the registry is heavily restricted - for good reasons - and it is very,very likely that you require Admin access in order to do very much at all; the likelyhood is that such access will become more restricted in future, not less. It is not recommended that anyone use the registry for new projects at all.

What is happening in your app is most likely to be that the undocumented function is failing because you don't have permission to do that and that the function "swallows" the error.

You could try this with an elevated app and see if that works, but to be honest you would be better off leaving the registry well allone and using a more modern settings file.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900