Solved

Writing, Removing and Searching Items in Windows Registery

Posted on 2004-04-05
9
353 Views
Last Modified: 2011-10-03
How Can I Do that (Question Title) ?
0
Comment
Question by:khalil_moghaddam
  • 5
  • 2
  • 2
9 Comments
 
LVL 7

Assisted Solution

by:sftweng
sftweng earned 20 total points
ID: 10763419
Have a look at the Delphi "TRegistry" class:

TRegistry is a low-level wrapper for the system registry and functions that operate on the registry.

Unit

Registry

Description

Use TRegistry to encapsulate access to the Windows system registry in an application. The registry is a database that an application can use to store and retrieve configuration information. Configuration information is stored in a hierarchical tree. Each node in the tree is called a key. Every key can contain subkeys and data values that represent part of the configuration information for an application.

All keys that an application creates, opens, reads, or writes are subkeys of predefined root keys. By default, a TRegistry object is created with a root key of HKEY_CURRENT_USER.

Only one key is accessible at a time in a TRegistry object. To determine the key that is currently accessible, read the value of the CurrentKey property. TRegistry methods enable an application to open, close, save, move, copy, and delete keys.

One or more data values containing actual configuration information can be stored in a key. TRegistry methods enable an application to query a key to see if it contains data, to read data in a key, and to write data to a key.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 80 total points
ID: 10765272
Create a new unit and call it 'UntReg'

unit UntReg;

interface

uses Windows, Registry, Classes, SysUtils;

procedure ReadRegistry;
procedure WriteRegistry;

var
 UserName: String;

implementation


// to write to the registry

procedure WriteRegistry;
var
 Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\Company Name\Application Name\', True) then
    begin
      Reg.WriteString('User Name', UserName);
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

// to read from the registry

procedure ReadRegistry;
var
 Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('\Software\<Company Name Here>\<Applicaion Name Here>\', True) then
    begin
     if Reg.ValueExists('USerName') then
      UserName := Reg.ReadString('UserName');
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

initialization
  begin
    ReadRegistry;
  end;

finalization
begin
  WriteRegistry;
end;

end.


Now from your main form (unit)

from the menu choose

File --> Use Unit

Choose Unit2



Note: There are other methods of the TRegistry object as well (such as ReadInteger, ReadBool, etc)
Look at the TRegistry Object in your help file for all of the available methods

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10765287
To search, you would have to know the key first , then test the key for the existence of a particular

first you open the key

 if Reg.OpenKey('\Software\<Company Name Here>\<Applicaion Name Here>\', True) then

then test for the existence

 if Reg.ValueExists('USerName') then
      UserName := Reg.ReadString('UserName');


Shane
0
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.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10765293
To delete,

use the

 Reg.DeleteKey & Reg.DeleteValue methods


Shane
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10765658
Quite a comprehensive answer for a 20-point question! :-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10765672
Cut & Paste (Isn't it great?)

Shane
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10766424

   Don't hardcode you strings like that ...

 create a unit with constants :

  unit MyAppConsts;
  const
     RegistryPath = '\Software\MyCompany\MyApplication\MyValue';
  ....

  then you need to defined the routine for read / write. Some of the guy above already did this, I would like to introduce some improvements - ability to read variants :

   uses MyAppConsts;
   ....
   function ReadRegistryValue (Name : String; ResType : TVarData) : Variant;
   var
      Reg : TRegistry;
   begin
      // open the registry
      Reg := TRegistry.Create;

      // open the key
      if Reg.OpenKey(RegistryPath, False) then
          begin
            case ResType of
              varInteger : Result := Reg.ReadInteger(Name);
              varString   : Result := Reg.ReadString(Name);
              varBoolean : Result := Reg.ReadBool(Name);
             // etc. there are other types, but these are enough
            end;
          end;

      // free the memory
      Reg.Free;
   end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10766482
<SMILE>

 I guess there was someone just as desperate for the 20 points

 <LOL>

Shane
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10772536

   not exactly ...

   The improvements :

   1) less lines of code
   2) more flexibility
   3) more useful

   <:P>
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 89
When I am typing into a TEdit last word repeats 7 128
delphi parse string to params 3 121
Convert MS Word document to a PDF file 9 78
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

10 Experts available now in Live!

Get 1:1 Help Now