Solved

reading and writing to windows registry

Posted on 2004-04-27
7
186 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month6 days, 10 hours left to enroll

636 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