Link to home
Start Free TrialLog in
Avatar of minckle
minckle

asked on

reading and writing to windows registry

please can some 1 point me in the right direction - websites etc.
  for learning how to read and write to the windows registry
Avatar of kretzschmar
kretzschmar
Flag of Germany image

read the delphi-online help about TRegistry
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

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
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.
Avatar of shaneholmes
shaneholmes

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.