Spostare a livello di codice le chiavi di registro
Domanda
Qualcuno sa come posso spostare programmaticamente un registro da HKEY_LOCAL_MCAHINE a HKEY_CURRENT_USER?
Ho scritto una funzione ricorsiva che utilizza RegEnumKeyEx e RegEnumValue, ma sembra che RegEnumValue restituisca tutti i valori sotto la chiave di livello superiore.
Ad esempio, se la chiave è HKEY_LOCAL_MACHINE \ SOFTWARE \ MyApp \ KeyName1 e ha 3 valori sotto di essa e ho HKEY_LOCAL_MACHINE \ SOFTWARE \ MyApp \ KeyName2 e che ha 2 valori. Sembra che RegEnumKeyEx stia restituendo le chiavi appropriate, ma quando chiamo RegEnumValue per la prima chiave (es. KeyName1) ottengo tutti i 5 valori restituiti e non solo i 3 sotto quella chiave.
Spero che tutto abbia un senso ... sto facendo qualcosa di sbagliato?
Grazie per l'aiuto
Ecco uno snippet, se aiuta:
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();
}
vale a dire. GetAllDataNames sopra chiama semplicemente RegEnumValue e GetAllSubKeys chiama RegEnumKeyEx.
Soluzione
Guardando attraverso tutte le funzioni del registro, ho trovato questo: SHCopyKey o posso usarlo: RegCopyTree per Vista e versioni successive.
Grazie per l'aiuto.
Altri suggerimenti
Non sono un esperto in questo, ma prova qualcosa del genere.
RegistryKey myKey = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\\" + csStartKey,
false);
Registry.CurrentUser.CreateSubKey
(myKey.ToString());