Solved

reading and writing to windows registry

Posted on 2004-04-27
7
172 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
Comment Utility
read the delphi-online help about TRegistry
0
 
LVL 17

Accepted Solution

by:
geobul earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now