Solved

Writing, Removing and Searching Items in Windows Registery

Posted on 2004-04-05
9
357 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
Industry Leaders: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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