?
Solved

Writing, Removing and Searching Items in Windows Registery

Posted on 2004-04-05
9
Medium Priority
?
361 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 60 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 240 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
Technology Partners: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

718 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