Solved

Writing, Removing and Searching Items in Windows Registery

Posted on 2004-04-05
9
358 Views
Last Modified: 2011-10-03
How Can I Do that (Question Title) ?
0
Comment
Question by:khalil_moghaddam
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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