Frantic
asked on
TRegistry
Has someone got an example of how to read and write a key from the registry.
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the easiest is to use the TRegIniFile. This is a wrapper around TRegistry in such a way that it looks like you're writing to an inifile (thus old programs saving to an ini-file can easely be changed to saving the registry) see regini example in help file!
unit reginid;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Registry;
type
TRegIniForm = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
CheckBox_Write: TCheckBox;
RadioGroup_Write: TRadioGroup;
Edit_Write: TEdit;
GroupBox2: TGroupBox;
Label3: TLabel;
CheckBox_Read: TCheckBox;
RadioGroup_Read: TRadioGroup;
Edit_Read: TEdit;
Memo_Read: TMemo;
Button_Write: TButton;
Button_Read: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button_WriteClick(Sender: TObject);
procedure Button_ReadClick(Sender: TObject);
procedure CheckBox_ReadClick(Sender: TObject);
procedure RadioGroup_ReadClick(Sende r: TObject);
private
{ Private declarations }
procedure Read_Registry;
procedure Write_Registry;
public
{ Public declarations }
FIniFile: TRegIniFile;
end;
var
RegIniForm: TRegIniForm;
implementation
{$R *.DFM}
const
SECTION = 'Sample';
procedure TRegIniForm.FormCreate(Sen der: TObject);
begin
FIniFile := TRegIniFile.Create('Sample Registry');
end;
procedure TRegIniForm.FormDestroy(Se nder: TObject);
var
Cleanup: TRegistry;
key: string;
begin
key := FIniFile.FileName;
FIniFile.Free;
// make sure we don't leave junk in the registry behind.
Cleanup := TRegistry.Create;
try
Cleanup.DeleteKey(key);
finally
Cleanup.Free;
end;
end;
procedure TRegIniForm.Write_Registry ;
begin
FIniFile.WriteBool(SECTION , 'BooleanTest', CheckBox_Write.Checked);
FIniFile.WriteInteger(SECT ION, 'IntegerTest', RadioGroup_Write.ItemIndex );
FIniFile.WriteString(SECTI ON, 'StringTest', Edit_Write.Text);
if not Button_Read.Enabled then Button_Read.Enabled := True;
end;
procedure TRegIniForm.Read_Registry;
begin
CheckBox_Read.Checked := FIniFile.ReadBool(SECTION, 'BooleanTest', False);
RadioGroup_Read.ItemIndex := FIniFile.ReadInteger(SECTI ON, 'IntegerTest', 0);
Edit_Read.Text := FIniFile.ReadString(SECTIO N, 'StringTest', '');
Memo_Read.Lines.Clear;
FIniFile.ReadSectionValues (SECTION, Memo_Read.Lines);
Button_Read.Enabled := False;
end;
procedure TRegIniForm.Button_WriteCl ick(Sender : TObject);
begin
Write_Registry;
end;
procedure TRegIniForm.Button_ReadCli ck(Sender: TObject);
begin
Read_Registry;
end;
procedure TRegIniForm.CheckBox_ReadC lick(Sende r: TObject);
begin
Read_Registry;
end;
procedure TRegIniForm.RadioGroup_Rea dClick(Sen der: TObject);
begin
Read_Registry;
end;
end.
Regards, Zif.
unit reginid;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Registry;
type
TRegIniForm = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
CheckBox_Write: TCheckBox;
RadioGroup_Write: TRadioGroup;
Edit_Write: TEdit;
GroupBox2: TGroupBox;
Label3: TLabel;
CheckBox_Read: TCheckBox;
RadioGroup_Read: TRadioGroup;
Edit_Read: TEdit;
Memo_Read: TMemo;
Button_Write: TButton;
Button_Read: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button_WriteClick(Sender: TObject);
procedure Button_ReadClick(Sender: TObject);
procedure CheckBox_ReadClick(Sender:
procedure RadioGroup_ReadClick(Sende
private
{ Private declarations }
procedure Read_Registry;
procedure Write_Registry;
public
{ Public declarations }
FIniFile: TRegIniFile;
end;
var
RegIniForm: TRegIniForm;
implementation
{$R *.DFM}
const
SECTION = 'Sample';
procedure TRegIniForm.FormCreate(Sen
begin
FIniFile := TRegIniFile.Create('Sample
end;
procedure TRegIniForm.FormDestroy(Se
var
Cleanup: TRegistry;
key: string;
begin
key := FIniFile.FileName;
FIniFile.Free;
// make sure we don't leave junk in the registry behind.
Cleanup := TRegistry.Create;
try
Cleanup.DeleteKey(key);
finally
Cleanup.Free;
end;
end;
procedure TRegIniForm.Write_Registry
begin
FIniFile.WriteBool(SECTION
FIniFile.WriteInteger(SECT
FIniFile.WriteString(SECTI
if not Button_Read.Enabled then Button_Read.Enabled := True;
end;
procedure TRegIniForm.Read_Registry;
begin
CheckBox_Read.Checked := FIniFile.ReadBool(SECTION,
RadioGroup_Read.ItemIndex := FIniFile.ReadInteger(SECTI
Edit_Read.Text := FIniFile.ReadString(SECTIO
Memo_Read.Lines.Clear;
FIniFile.ReadSectionValues
Button_Read.Enabled := False;
end;
procedure TRegIniForm.Button_WriteCl
begin
Write_Registry;
end;
procedure TRegIniForm.Button_ReadCli
begin
Read_Registry;
end;
procedure TRegIniForm.CheckBox_ReadC
begin
Read_Registry;
end;
procedure TRegIniForm.RadioGroup_Rea
begin
Read_Registry;
end;
end.
Regards, Zif.
Read :
with TRegistry.Create do begin
RootKey := HKEY_LOCAL_MACHINE; { HKEY_CURRENT_USER by default }
if OpenKey('Software\Microsof t\Windows\ help', false) then begin
ShowMessage(ReadString('Da o35.hlp')) ;
CloseKey;
end;
Free;
end;
Write :
procedure TForm1.Button1Click(Sender : TObject);
var
reg : TRegistry;
begin
reg := TRegistry.Create;
reg.RootKey := HKEY_USERS; { set root }
reg.OpenKey('geo',true); { open a key or create it }
reg.WriteInteger('gInt',12 3456); { write unsigned DWORD }
reg.WriteBool('gBool',true );
reg.WriteFloat('gFloat',12 3456.34); { write binary data }
reg.Free;
end;
Regards, Geo
with TRegistry.Create do begin
RootKey := HKEY_LOCAL_MACHINE; { HKEY_CURRENT_USER by default }
if OpenKey('Software\Microsof
ShowMessage(ReadString('Da
CloseKey;
end;
Free;
end;
Write :
procedure TForm1.Button1Click(Sender
var
reg : TRegistry;
begin
reg := TRegistry.Create;
reg.RootKey := HKEY_USERS; { set root }
reg.OpenKey('geo',true); { open a key or create it }
reg.WriteInteger('gInt',12
reg.WriteBool('gBool',true
reg.WriteFloat('gFloat',12
reg.Free;
end;
Regards, Geo
//Read
procedure TForm1.Button1Click(Sender : TObject);
var
myreg : treginifile;
S1 : String;
begin
MyReg := TregIniFile.create('');
MyReg.RootKey := Hkey_Local_Machine;
s1 := MyReg.ReadString('Test', 'Dark_king', '');
myreg.free;
edit1.Text:= s1;
end;
//Write
procedure TForm1.Button2Click(Sender : TObject);
var
Reg: TRegistry;
adregtext: String;
begin
Reg := TRegistry.Create;
adregtext :='\Test'; // HKEY_LOCAL_MACHINE\Test
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey(adregtext, True)
then Reg.WriteString('Dark_king ','Hear it is');
finally
Reg.CloseKey;
Reg.Free;
end;
end;
procedure TForm1.Button1Click(Sender
var
myreg : treginifile;
S1 : String;
begin
MyReg := TregIniFile.create('');
MyReg.RootKey := Hkey_Local_Machine;
s1 := MyReg.ReadString('Test', 'Dark_king', '');
myreg.free;
edit1.Text:= s1;
end;
//Write
procedure TForm1.Button2Click(Sender
var
Reg: TRegistry;
adregtext: String;
begin
Reg := TRegistry.Create;
adregtext :='\Test'; // HKEY_LOCAL_MACHINE\Test
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey(adregtext, True)
then Reg.WriteString('Dark_king
finally
Reg.CloseKey;
Reg.Free;
end;
end;
In your questions you want to read the KEY not the VALUE in register
So if you want to read key it can bee more an one key
This example read all keys under
HKEY_LOCAL_MACHINE\Config\ 0001\Syste m\CurrentC ontrolSet\ Control\Pr int\Printe rs
procedure TForm1.Button1Click(Sender : TObject);
var
Reg3: TRegistry;
deltext: String;
Val:TStringList;
I: Integer;
j:String;
begin
deltext :='\Config\0001\System\Cur rentContro lSet\Contr ol\Print\P rinters';
Reg3 := TRegistry.Create;
try
Reg3.RootKey := HKEY_LOCAL_MACHINE;
if Reg3.OpenKey(deltext,False ) then
Val:=TStringList.Create;
reg3.GetKeyNames(Val);
for I:=0 to Val.Count-1 do
begin
j:=Val.Strings[I] ;
ListBox1.items.Add(Val.Str ings[I]) ;
end;
finally
Val.Free;
Reg3.CloseKey;
Reg3.Free;
inherited;
end;
end;
So if you want to read key it can bee more an one key
This example read all keys under
HKEY_LOCAL_MACHINE\Config\
procedure TForm1.Button1Click(Sender
var
Reg3: TRegistry;
deltext: String;
Val:TStringList;
I: Integer;
j:String;
begin
deltext :='\Config\0001\System\Cur
Reg3 := TRegistry.Create;
try
Reg3.RootKey := HKEY_LOCAL_MACHINE;
if Reg3.OpenKey(deltext,False
Val:=TStringList.Create;
reg3.GetKeyNames(Val);
for I:=0 to Val.Count-1 do
begin
j:=Val.Strings[I] ;
ListBox1.items.Add(Val.Str
end;
finally
Val.Free;
Reg3.CloseKey;
Reg3.Free;
inherited;
end;
end;
ASKER
Thanks
// Just read language from regestry
GetLocaleInfo( LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, @LocaleStr, sizeof(LocaleStr) );