fibdev
asked on
read and write the registry
Hello,
This is an easy one... How do I read and write the registry. I need to know how to do strings and integers
- Thanks
This is an easy one... How do I read and write the registry. I need to know how to do strings and integers
- Thanks
ASKER
Thanks Mike, can you show me examples of:
reading a string
writing a string
reading a integer
writing a integer
I would give more points, but I'm out :)
- Gabe
reading a string
writing a string
reading a integer
writing a integer
I would give more points, but I'm out :)
- Gabe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I hope this helps:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, registry,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
reg : TRegIniFile;
begin
reg := TRegIniFile.create(applica tion.exeNa me);
left := reg.readInteger('prefs','l eft',0);
top := reg.readInteger('prefs','t op',0);
caption := reg.readString('prefs','ca ption','Mi ke''s TRegIniFile Example App');
reg.Free;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
reg : TRegIniFile;
begin
reg := TRegIniFile.create(applica tion.exeNa me);
reg.writeInteger('prefs',' left',left );
reg.writeInteger('prefs',' top',top);
reg.writeString('prefs','c aption',ca ption);
reg.Free;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
caption := edit1.text;
end;
end.
GL
Mike
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, registry,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
reg : TRegIniFile;
begin
reg := TRegIniFile.create(applica
left := reg.readInteger('prefs','l
top := reg.readInteger('prefs','t
caption := reg.readString('prefs','ca
reg.Free;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
reg : TRegIniFile;
begin
reg := TRegIniFile.create(applica
reg.writeInteger('prefs','
reg.writeInteger('prefs','
reg.writeString('prefs','c
reg.Free;
end;
procedure TForm1.Button1Click(Sender
begin
caption := edit1.text;
end;
end.
GL
Mike
Hi fibdev,
Here is a example for reading and writing:
uses registry;
procedure WriteTheRegistry;
var Reg:TRegistry;
begin
Reg:=Tregistry.Create;
Reg.RootKey:=HKEY_LOCAL_MA CHINE;
if Reg.OpenKey('Software\Your s\Setting' ,true) then begin
Reg.WriteString('StringVal ue','Setti ng');
Reg.WriteInteger('IntValue ',3);
Reg.CloseKey;
end;
Reg.Free;
end;
procedure ReadTheRegistry;
var Reg:TRegistry;
S:String;
begin
Reg:=Tregistry.Create;
Reg.RootKey:=HKEY_LOCAL_MA CHINE;
if Reg.KeyExists('Software\Yo urs\Settin g') then begin
Reg.OpenKeyReadOnly('Softw are\Yours\ Setting');
if Reg.ValueExists('StringVal ue') then
S:=Reg.ReadString('StringV alue');
Reg.CloseKey;
end;
end;
Regards,
HakanB
Here is a example for reading and writing:
uses registry;
procedure WriteTheRegistry;
var Reg:TRegistry;
begin
Reg:=Tregistry.Create;
Reg.RootKey:=HKEY_LOCAL_MA
if Reg.OpenKey('Software\Your
Reg.WriteString('StringVal
Reg.WriteInteger('IntValue
Reg.CloseKey;
end;
Reg.Free;
end;
procedure ReadTheRegistry;
var Reg:TRegistry;
S:String;
begin
Reg:=Tregistry.Create;
Reg.RootKey:=HKEY_LOCAL_MA
if Reg.KeyExists('Software\Yo
Reg.OpenKeyReadOnly('Softw
if Reg.ValueExists('StringVal
S:=Reg.ReadString('StringV
Reg.CloseKey;
end;
end;
Regards,
HakanB
ASKER
Ok...
I don't know which one to use :)
What is this:
reg := TRegIniFile.create(applica tion.exeNa me);
Is this ini or registry? And what is the application.exename part for?
I understand HakanB's example but I want to give you a chance to finish your suggestion Mike.
I don't know which one to use :)
What is this:
reg := TRegIniFile.create(applica
Is this ini or registry? And what is the application.exename part for?
I understand HakanB's example but I want to give you a chance to finish your suggestion Mike.
TRegIniFile is an object that lets you read/write to/from the registry like it was an ini file. The is no ini file involved, it just appears that way to the developer. This make's it very easy for those who are familiar with .ini's to migrate to the registry. The parameter in the constructor merely gives the TRegIniFile a unique string with wich to name a registry key to stuff all of your info in.
The help says it quite well:
"TRegIniFile is a low-level wrapper for the Windows 95/NT system registry that is intended to enable existing Delphi applications that used INI files to use the system registry with a minimum of coding changes."
and:
"he FileName passed to a TRegIniFile object becomes a subkey under the system registry’s root key (HKEY_CURRENT_USER by default). What corresponds to a section in an INI file is treated as a key in the system registry, and what corresponds to data entries under a section in an INI file are treated as data values under a key in the system registry."
GL
Mike
The help says it quite well:
"TRegIniFile is a low-level wrapper for the Windows 95/NT system registry that is intended to enable existing Delphi applications that used INI files to use the system registry with a minimum of coding changes."
and:
"he FileName passed to a TRegIniFile object becomes a subkey under the system registry’s root key (HKEY_CURRENT_USER by default). What corresponds to a section in an INI file is treated as a key in the system registry, and what corresponds to data entries under a section in an INI file are treated as data values under a key in the system registry."
GL
Mike
fibdev,
inthe is the first with the right answer. didn't you have a look at it?
cheers.
inthe is the first with the right answer. didn't you have a look at it?
cheers.
;-)))
>> inthe is the first with the right answer <<
as usual...
apart from that he seems to be one of only a handful who are familiar w/ try...finally blocks.
cheers,
BlackDeath.
>> inthe is the first with the right answer <<
as usual...
apart from that he seems to be one of only a handful who are familiar w/ try...finally blocks.
cheers,
BlackDeath.
Fibdev,
as BlackDeath and Mahara stated inthe is the first with the answer although other answers are right also.
I hope your problem is solved.
:)
HakanB
as BlackDeath and Mahara stated inthe is the first with the answer although other answers are right also.
I hope your problem is solved.
:)
HakanB
well, no actually. Inthe was the first to provide an example, after I suggested a method, and slightly before my solicited example was given.
Gl
Mike
Gl
Mike
mike. this is like:
q: how do i drive a car?
a: perhaps the easiest way of learning how to drive a car is to use a chevrolet - it's stinkin' easy :)
anyway.
have a nice day, all.
i'm off now.
BlackDeath.
q: how do i drive a car?
a: perhaps the easiest way of learning how to drive a car is to use a chevrolet - it's stinkin' easy :)
anyway.
have a nice day, all.
i'm off now.
BlackDeath.
Hi, there,
Please create the utils.pas first and then you can use the fuctions in your application many times.
unit Utils;
interface
uses
Windows, registry, sysUtils, stdctrls, Controls ;
type
TDoRegistry = class(TObject)
private
FRegistryKey : string ;
public
constructor Create(const sRegistryKey : string ) ;
destructor destroy ; reintroduce ;
procedure SavestrToRegistry( sKeyName, sSaveName, sSaveValue : string );
function LoadstrFromRegistry(sKeyNa me, sSaveName : string ) : string ;
procedure SaveintToRegistry( sKeyName, sSaveName : string; sSaveValue :integer);
function LoadintFromRegistry(sKeyNa me, sSaveName : string ) : integer ;
end ;
constructor TDoRegistry.Create( const sRegistryKey : string );
begin
inherited Create;
FRegistryKey := sRegistryKey ;
end ;
destructor TDoRegistry.Destroy ;
begin
inherited Destroy ;
end ;
procedure TDoRegistry.SaveStrToRegis try( sKeyName, sSaveName, sSaveValue : string );
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteString( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadStrFromReg istry(sKey Name, sSaveName : string ) : string ;
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN E;
RootKey:=HKEY_CURRENT_USER ;
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadString(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
procedure TDoRegistry.SaveIntToRegis try( sKeyName, sSaveName : string;sSaveValue:Integer );
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteInteger( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadIntFromReg istry(sKey Name, sSaveName : string ) : Integer ;
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN E;
RootKey:=HKEY_CURRENT_USER ;
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadInteger(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
//simple exmaple:
Unit APPNAME;
....
uses Utils;
....
procedure Login
const
PROGRAM_REG_KEY = '\Software\COMPANYNAME\APP NAME' ;
var
DoRegistry : TDoRegistry;
begin
DoReg := TDoReg.Create(PROGRAM_REG_ KEY) ;
DoRegistry.SaveStrToRegist ry(PROGRAM _REG_KEY,' TCPPort',' 127.0.0.1' ); //localhost
Showmessage( DoRegistry.LoadStrFromRegi stry(PROGR AM_REG_KEY ,'TCPPort' ));
DoReg.Free;
end;
Hope that it is useful for you.
Jing
Please create the utils.pas first and then you can use the fuctions in your application many times.
unit Utils;
interface
uses
Windows, registry, sysUtils, stdctrls, Controls ;
type
TDoRegistry = class(TObject)
private
FRegistryKey : string ;
public
constructor Create(const sRegistryKey : string ) ;
destructor destroy ; reintroduce ;
procedure SavestrToRegistry( sKeyName, sSaveName, sSaveValue : string );
function LoadstrFromRegistry(sKeyNa
procedure SaveintToRegistry( sKeyName, sSaveName : string; sSaveValue :integer);
function LoadintFromRegistry(sKeyNa
end ;
constructor TDoRegistry.Create( const sRegistryKey : string );
begin
inherited Create;
FRegistryKey := sRegistryKey ;
end ;
destructor TDoRegistry.Destroy ;
begin
inherited Destroy ;
end ;
procedure TDoRegistry.SaveStrToRegis
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteString( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadStrFromReg
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN
RootKey:=HKEY_CURRENT_USER
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadString(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
procedure TDoRegistry.SaveIntToRegis
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteInteger( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadIntFromReg
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN
RootKey:=HKEY_CURRENT_USER
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadInteger(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
//simple exmaple:
Unit APPNAME;
....
uses Utils;
....
procedure Login
const
PROGRAM_REG_KEY = '\Software\COMPANYNAME\APP
var
DoRegistry : TDoRegistry;
begin
DoReg := TDoReg.Create(PROGRAM_REG_
DoRegistry.SaveStrToRegist
Showmessage( DoRegistry.LoadStrFromRegi
DoReg.Free;
end;
Hope that it is useful for you.
Jing
Hi, there,
Please create the utils.pas first and then you can use the fuctions in your application many times.
unit Utils;
interface
uses
Windows, registry, sysUtils, stdctrls, Controls ;
type
TDoRegistry = class(TObject)
private
FRegistryKey : string ;
public
constructor Create(const sRegistryKey : string ) ;
destructor destroy ; reintroduce ;
procedure SavestrToRegistry( sKeyName, sSaveName, sSaveValue : string );
function LoadstrFromRegistry(sKeyNa me, sSaveName : string ) : string ;
procedure SaveintToRegistry( sKeyName, sSaveName : string; sSaveValue :integer);
function LoadintFromRegistry(sKeyNa me, sSaveName : string ) : integer ;
end ;
constructor TDoRegistry.Create( const sRegistryKey : string );
begin
inherited Create;
FRegistryKey := sRegistryKey ;
end ;
destructor TDoRegistry.Destroy ;
begin
inherited Destroy ;
end ;
procedure TDoRegistry.SaveStrToRegis try( sKeyName, sSaveName, sSaveValue : string );
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteString( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadStrFromReg istry(sKey Name, sSaveName : string ) : string ;
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN E;
RootKey:=HKEY_CURRENT_USER ;
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadString(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
procedure TDoRegistry.SaveIntToRegis try( sKeyName, sSaveName : string;sSaveValue:Integer );
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteInteger( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadIntFromReg istry(sKey Name, sSaveName : string ) : Integer ;
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN E;
RootKey:=HKEY_CURRENT_USER ;
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadInteger(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
//simple exmaple:
Unit APPNAME;
....
uses Utils;
....
procedure Login
const
PROGRAM_REG_KEY = '\Software\COMPANYNAME\APP NAME' ;
var
DoRegistry : TDoRegistry;
begin
DoReg := TDoReg.Create(PROGRAM_REG_ KEY) ;
DoRegistry.SaveStrToRegist ry(PROGRAM _REG_KEY,' TCPPort',' 127.0.0.1' ); //localhost
Showmessage( DoRegistry.LoadStrFromRegi stry(PROGR AM_REG_KEY ,'TCPPort' ));
DoReg.Free;
end;
Hope that it is useful for you.
Jing
Please create the utils.pas first and then you can use the fuctions in your application many times.
unit Utils;
interface
uses
Windows, registry, sysUtils, stdctrls, Controls ;
type
TDoRegistry = class(TObject)
private
FRegistryKey : string ;
public
constructor Create(const sRegistryKey : string ) ;
destructor destroy ; reintroduce ;
procedure SavestrToRegistry( sKeyName, sSaveName, sSaveValue : string );
function LoadstrFromRegistry(sKeyNa
procedure SaveintToRegistry( sKeyName, sSaveName : string; sSaveValue :integer);
function LoadintFromRegistry(sKeyNa
end ;
constructor TDoRegistry.Create( const sRegistryKey : string );
begin
inherited Create;
FRegistryKey := sRegistryKey ;
end ;
destructor TDoRegistry.Destroy ;
begin
inherited Destroy ;
end ;
procedure TDoRegistry.SaveStrToRegis
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteString( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadStrFromReg
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN
RootKey:=HKEY_CURRENT_USER
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadString(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
procedure TDoRegistry.SaveIntToRegis
var
reg : TRegistry ;
begin
if sSaveName <> '' then begin
reg := TRegistry.Create ;
with reg do begin
// RootKey := HKEY_LOCAL_MACHINE; //if different users use the same registry keys.
RootKey := HKEY_CURRENT_USER;
try
if OpenKey(sKeyName,true) then begin
WriteInteger( sSaveName, sSaveValue );
CloseKey;
end;
finally
Free ;
end ;
end ;
end;
end;
function TDoRegistry.LoadIntFromReg
var
reg : TRegistry ;
begin
reg :=TRegistry.Create;
with reg do begin
// RootKey:=HKEY_LOCAL_MACHIN
RootKey:=HKEY_CURRENT_USER
try
if not OpenKeyReadOnly(sKeyName) then begin
Createkey(sKeyname);
end ;
result := ReadInteger(sSaveName);
CloseKey ;
finally
Free ;
end;
end ;
end ;
//simple exmaple:
Unit APPNAME;
....
uses Utils;
....
procedure Login
const
PROGRAM_REG_KEY = '\Software\COMPANYNAME\APP
var
DoRegistry : TDoRegistry;
begin
DoReg := TDoReg.Create(PROGRAM_REG_
DoRegistry.SaveStrToRegist
Showmessage( DoRegistry.LoadStrFromRegi
DoReg.Free;
end;
Hope that it is useful for you.
Jing
ASKER
Comment accepted as answer
ASKER
Thanks :)
GL
Mike