Solved

TRegistry

Posted on 2000-02-21
7
498 Views
Last Modified: 2010-04-04
Has someone got an example of how to read and write a key from the registry.

Thanks
0
Comment
Question by:Frantic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Accepted Solution

by:
raidos earned 100 total points
ID: 2545082
I believe this should cover it.


Procedure TForm1.Button1Click(Sender: TObject);
Var
 S : String;
 Reg : TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\Path to Registry key\', False) then Begin
         S := Reg.ReadString('Keyname');
         Reg.WriteString('Keyname','Store this');
     End;
  finally
    Reg.CloseKey;
    Reg.Free;
  end;
end;

Good luck
//raidos
0
 

Expert Comment

by:MaratX
ID: 2545249
// For next example
// Just read language from regestry
  GetLocaleInfo( LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, @LocaleStr, sizeof(LocaleStr) );
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2545387
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(Sender: 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(Sender: TObject);
begin
  FIniFile := TRegIniFile.Create('Sample Registry');
end;

procedure TRegIniForm.FormDestroy(Sender: 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(SECTION, 'IntegerTest', RadioGroup_Write.ItemIndex);

  FIniFile.WriteString(SECTION, '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(SECTION, 'IntegerTest', 0);
  Edit_Read.Text := FIniFile.ReadString(SECTION, 'StringTest', '');

  Memo_Read.Lines.Clear;

  FIniFile.ReadSectionValues(SECTION, Memo_Read.Lines);

  Button_Read.Enabled := False;
end;

procedure TRegIniForm.Button_WriteClick(Sender: TObject);
begin
  Write_Registry;
end;

procedure TRegIniForm.Button_ReadClick(Sender: TObject);
begin
  Read_Registry;
end;


procedure TRegIniForm.CheckBox_ReadClick(Sender: TObject);
begin
  Read_Registry;
end;

procedure TRegIniForm.RadioGroup_ReadClick(Sender: TObject);

begin
  Read_Registry;
end;

end.

Regards, Zif.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:geobul
ID: 2545454
Read :

with TRegistry.Create do begin
    RootKey := HKEY_LOCAL_MACHINE; { HKEY_CURRENT_USER by default }
    if OpenKey('Software\Microsoft\Windows\help', false) then begin
      ShowMessage(ReadString('Dao35.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',123456); { write unsigned DWORD }
  reg.WriteBool('gBool',true);
  reg.WriteFloat('gFloat',123456.34); { write binary data }
  reg.Free;
end;

Regards, Geo
0
 
LVL 6

Expert Comment

by:Dark_King
ID: 2545605
//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;
0
 
LVL 6

Expert Comment

by:Dark_King
ID: 2545889
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\System\CurrentControlSet\Control\Print\Printers


procedure TForm1.Button1Click(Sender: TObject);
var
Reg3: TRegistry;
deltext: String;
Val:TStringList;
  I: Integer;
      j:String;
begin
   deltext :='\Config\0001\System\CurrentControlSet\Control\Print\Printers';
  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.Strings[I]) ;
         end;
           finally
  Val.Free;
  Reg3.CloseKey;
  Reg3.Free;
    inherited;
    end;
end;
0
 

Author Comment

by:Frantic
ID: 2545960
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question