Solved

reading and writing to windows registry

Posted on 2004-04-27
7
174 Views
Last Modified: 2010-04-05
please can some 1 point me in the right direction - websites etc.
  for learning how to read and write to the windows registry
0
Comment
Question by:minckle
7 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10927918
read the delphi-online help about TRegistry
0
 
LVL 17

Accepted Solution

by:
geobul earned 125 total points
ID: 10927954
Hi,

// using registry to save/read your application settings:

uses registry;

var
  fnt: string;
  clr: TColor;

procedure ReadSettings;
begin
  with TRegistry.Create do begin
    if OpenKey('Software\YourApp', false) then begin
      fnt := ReadString('Font');
      clr := ReadInteger('Color'); // see other ReadXXX methods of TRegistry in your Delphi help
      CloseKey;
    end;
    Free;
  end;
end;

procedure SaveSettings;
begin
  with TRegistry.Create do begin
    if OpenKey('Software\YourApp', true) then begin
      WriteString('Font', fnt);
      WriteInteger('Color', clr); // see other WriteXXX methods of TRegistry in your Delphi help
      CloseKey;
    end;
    Free;
  end;
end;

Regards, Geo
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10928049
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Registry;

const
  AppRegPath = '\Software\YourCompany\YouApp';

type
  VarTypes = (vtInteger, vtString, vtBoolean, vtDate, vtTime, vtDateTime);

  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

// SettingType = vtInteger if you want to read string
// vtString - for String
// vtBoolean - for Boolean etc
function ReadSetting (SettingName : String; SettingType : VarTypes) : Variant;
var
  Reg     : TRegistry;
begin
  Reg := TRegistry.Create;
  if Reg.OpenKey(AppRegPath, false) then
    case SettingType of
      vtInteger       : Result := Reg.ReadInteger(SettingName);
      vtString        : Result := Reg.ReadString(SettingName);
      vtBoolean       : Result := Reg.ReadBool(SettingName);
    end;
end;

end.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10928060
Here are some articles to help explain more..... and a unit i created to go with it.

http://delphi.about.com/gi/dynamic/offsite.htm?site=http://www.aimtec.com.au/articles/RegistryInDelphi/default.htm

http://delphi.about.com/gi/dynamic/offsite.htm?site=http://www.geocities.com/SiliconValley/Lakes/1636/registry.htm

http://delphi.about.com/library/weekly/aa011299.htm


You must first make sure you have Uses  Registry....


unit UntCommon;

interface

uses Windows, Classes, SysUtils, Registry;



procedure ReadRegistry;
procedure WriteRegistry;


var

 EnableAlarm: Boolean;
 AlarmFile: String;
 RegDate: TDateTime;
 TaxRate: Real;

implementation



procedure WriteRegistry;
var
 Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\Acme\GCM\', True) then
    begin
     Reg.WriteDate('Reg Date', RegDate);
     Reg.WriteBool('Enable Alarm', EnableAlarm);
     Reg.WriteSTring('Alarm'File, AlarmFile);
     Reg.WriteBool('Enable Tax', EnableTax);
     Reg.WriteFloat('Tax Rate', TaxRate);
     Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

procedure ReadRegistry;
var
 Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\Acme\GCM\', True) then
    begin
     if Reg.ValueExists('Enable Alarm') then
      EnableAlarm:= Reg.ReadBool('Enable Alarm')
     else
      EnableAlarm:= True;
     if Reg.ValueExists('Enable Tax') then
      EnableTax:= Reg.ReadBool('Enable Tax')
     else
      EnableTax:= False;
     if Reg.ValueExists('Alarm File') then
      AlarmFile:= Reg.ReadString('Alarm File')
     else
      AlarmFile:= ExtractFilePath(Application.ExeName) + 'Alarm.wav';
     if Reg.ValueExists('Tax Rate') then
      TaxRate:= Reg.ReadFloat('Tax Rate')
     else
      TaxRate:= 0.07;
     if Reg.ValueExists('Reg Date') then
      RegDate:= Reg.ReadDate('Reg Date')
     else
      RegDate:= Now;
     Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

end.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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