Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading and writing to windows registry

Posted on 2004-04-27
7
Medium Priority
?
194 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
[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 27

Expert Comment

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

Accepted Solution

by:
geobul earned 500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

730 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