Move registry keys programmatically

Does anyone know how I can programmatically move the registry from HKEY_LOCAL_MCAHINE to HKEY_CURRENT_USER?

I wrote a recursive function that uses RegEnumKeyEx and RegEnumValue, but it looks like RegEnumValue returns all values ​​under the top-level key.

For example, if the key is HKEY_LOCAL_MACHINE \ SOFTWARE \ MyApp \ KeyName1 and has 3 values ​​under it and I have HKEY_LOCAL_MACHINE \ SOFTWARE \ MyApp \ KeyName2 and has 2 values. It looks like RegEnumKeyEx is returning the correct keys, but when I call RegEnumValue on the first key (i.e. KeyName1), I get all 5 values, not just 3 under that key.

Hope this all makes sense ... Am I doing something wrong?

Thanks for any help

Here's a snippet if it helps:

void CArgusApp::RecurseSubKeys(CString csStartKey)
{
    CQERegistry reg;

    HRESULT hr = reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\" + csStartKey, KEY_QUERY_VALUE );

    CStringArray csaDataNames;
    reg.GetAllDataNames(csaDataNames);
    for (int j = 0; j < csaDataNames.GetSize(); j++)
    {
        CString csValueName = csaDataNames.ElementAt(j);
        TRACE(csStartKey + " - " + csValueName);
    }

    CStringArray csaKeys;
    reg.GetAllSubKeys(csaKeys);
    for (int i = 0; i < csaKeys.GetSize(); i++)
    {
        CString csKey = csaKeys.ElementAt(i);
        this->RecurseSubKeys(csStartKey + "\\" + csKey);
    }

    reg.Close();
}

      

i.e. GetAllDataNames above just calls RegEnumValue and GetAllSubKeys calls RegEnumKeyEx.

+2


source to share


2 answers


Going through all the registry functions I found this: SHCopyKey or I can use this: RegCopyTree for Vista and later.



Thanks for the help.

+3


source


I am not an expert on this, but I will try something like this.



  RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
  @"SOFTWARE\\" + csStartKey, 
  false);
Registry.CurrentUser.CreateSubKey
        (myKey.ToString());

      

0


source







All Articles