Link to home
Start Free TrialLog in
Avatar of fibdev
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
Avatar of edey
edey

perhaps the easiest way of storing your own info in the reg. is to use the TRegIniFile object - it's stinkin' easy :)

GL
Mike
Avatar of fibdev

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
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(application.exeName);
     left := reg.readInteger('prefs','left',0);
     top := reg.readInteger('prefs','top',0);
     caption := reg.readString('prefs','caption','Mike''s TRegIniFile Example App');
     reg.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
   reg : TRegIniFile;
begin
     reg := TRegIniFile.create(application.exeName);
     reg.writeInteger('prefs','left',left);
     reg.writeInteger('prefs','top',top);
     reg.writeString('prefs','caption',caption);
     reg.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
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_MACHINE;
   if Reg.OpenKey('Software\Yours\Setting',true) then begin
      Reg.WriteString('StringValue','Setting');
      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_MACHINE;
   if Reg.KeyExists('Software\Yours\Setting') then begin
      Reg.OpenKeyReadOnly('Software\Yours\Setting');          
      if Reg.ValueExists('StringValue') then
          S:=Reg.ReadString('StringValue');
      Reg.CloseKey;
   end;
end;

Regards,
HakanB

Avatar of fibdev

ASKER

Ok...

I don't know which one to use :)

What is this:

reg := TRegIniFile.create(application.exeName);

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
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 <<

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
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
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.

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(sKeyName, sSaveName : string ) : string ;
         procedure SaveintToRegistry( sKeyName, sSaveName : string; sSaveValue :integer);
         function LoadintFromRegistry(sKeyName, 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.SaveStrToRegistry( 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.LoadStrFromRegistry(sKeyName, sSaveName : string ) : string ;
var
    reg : TRegistry ;
begin
    reg :=TRegistry.Create;
    with reg do begin
//         RootKey:=HKEY_LOCAL_MACHINE;
         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.SaveIntToRegistry( 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.LoadIntFromRegistry(sKeyName, sSaveName : string ) : Integer ;
var
    reg : TRegistry ;
begin
    reg :=TRegistry.Create;
    with reg do begin
//         RootKey:=HKEY_LOCAL_MACHINE;
         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\APPNAME' ;
var
    DoRegistry : TDoRegistry;
begin
    DoReg := TDoReg.Create(PROGRAM_REG_KEY) ;  
    DoRegistry.SaveStrToRegistry(PROGRAM_REG_KEY,'TCPPort','127.0.0.1');  //localhost
    Showmessage( DoRegistry.LoadStrFromRegistry(PROGRAM_REG_KEY,'TCPPort'));
    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(sKeyName, sSaveName : string ) : string ;
         procedure SaveintToRegistry( sKeyName, sSaveName : string; sSaveValue :integer);
         function LoadintFromRegistry(sKeyName, 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.SaveStrToRegistry( 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.LoadStrFromRegistry(sKeyName, sSaveName : string ) : string ;
var
    reg : TRegistry ;
begin
    reg :=TRegistry.Create;
    with reg do begin
//         RootKey:=HKEY_LOCAL_MACHINE;
         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.SaveIntToRegistry( 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.LoadIntFromRegistry(sKeyName, sSaveName : string ) : Integer ;
var
    reg : TRegistry ;
begin
    reg :=TRegistry.Create;
    with reg do begin
//         RootKey:=HKEY_LOCAL_MACHINE;
         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\APPNAME' ;
var
    DoRegistry : TDoRegistry;
begin
    DoReg := TDoReg.Create(PROGRAM_REG_KEY) ;  
    DoRegistry.SaveStrToRegistry(PROGRAM_REG_KEY,'TCPPort','127.0.0.1');  //localhost
    Showmessage( DoRegistry.LoadStrFromRegistry(PROGRAM_REG_KEY,'TCPPort'));
    DoReg.Free;
end;

Hope that it is useful for you.

Jing
Avatar of fibdev

ASKER

Comment accepted as answer
Avatar of fibdev

ASKER

Thanks :)