Title: The three ways to backup or restore Windows Registry.
Question: How to backup and restore all registry or one branch of registry?
Please read ...
Answer:
No 1.use regedit uses shellapi
shellexecute(handle,'open','regedit.exe',' /e YourWantExportKey YourBackUpFilename','',sw_hide)
No 2.Use windoews API RegSaveKey or RegRestoreKey
The RegSaveKey function saves the specified key and all of its subkeys
and values to a new file.
LONG RegSaveKey(
HKEY hKey, // handle of key where save begins
LPCTSTR lpFile, // address of filename to save to
LPSECURITY_ATTRIBUTES lpSecurityAttributes // address of security structure
);
The RegRestoreKey function reads the registry information in a specified file and copies it over the specified key. This registry information may be in the form of a key and multiple levels of subkeys.
LONG RegRestoreKey(
HKEY hKey, // handle of key where restore begins
LPCTSTR lpFile, // address of filename containing saved tree
DWORD dwFlags // optional flags
);
example code:
procedure TFormMain.ButtonExportClick(Sender: TObject);
var
RootKey,phKey: hKey;
KeyName,sKeyFileName: String;
FileName: array [0..255] of char;
begin
RootKey := HKEY_CURRENT_USER;
KeyName := 'software\mysoft\abc';
RegOpenKeyEx(RootKey, PChar(KeyName), 0, KEY_ALL_ACCESS, phKey);
sKeyFileName := 'c:\tempReg';
StrPCopy(FileName,sKeyFileName); //or use pchar
if RegSaveKey(phKey, FileName, nil)= 0 then
ShowMessage('BACKUP OK!')
else
ShowMessage('BACKUP ERROR!');
RegCloseKey(phKey);
end;
procedure TFormMain.ButtonImportClick(Sender: TObject);
var //Restore from file
RootKey,phKey: hKey;
KeyName,sKeyFileName: String;
FileName: array [0..255] of char;
begin
RootKey := HKEY_CURRENT_USER;
KeyName := 'software\mySoft\abc';
RegOpenKeyEx(RootKey, PChar(KeyName), 0, KEY_ALL_ACCESS, phKey);
sKeyFileName := 'c:\tempReg';
StrPCopy(FileName,sKeyFileName);
if RegRestoreKey(phKey, FileName,0)= 0 then
ShowMessage('RESTORE OK!')
else
ShowMessage('RESTORE ERROR!');
RegCloseKey(phKey);
end;
No 3.Use TRegistry's savekey and restorekey
var
reg : Tregistry;
begin
reg := Tregistry.Create;
reg.rootkey := HKEY_CURRENT_USER;
reg.Savekey('\Software\Wom','d:\test1\Wom');
end;