中國人民銀行鄭州培訓學院 信息部 潘漢杰
Windows注冊表跟蹤記錄了操作系統和各種在系統上安裝的應用程序的有關信息。注冊表是一個分層數據庫。該數據庫包含以特定格式表示的配置信息,并且可以直接通過注冊表編輯器程序編輯,也可以通過應用程序修改。在早期的Windows版本中,INI文件粗略地完成了這一功能,盡管注冊表與INI文件有一些相似之處,但不管怎樣,由于組織結構的原因,注冊表比傳統的INI文件要復雜得多。 注冊表分成若干個主要部分,各個部分(或者說關鍵字)包含了不同種類的系統信息。比如說,當你創建一個應用程序時,你可以在HKEY_LOCAL_MACHINE這個部分的SOFTWARE子節中注冊你的配置信息。應用程序可以把配置信息以多種數據類型(包括字符串、貨幣、日期、浮點數和布爾值)的形式保存在關鍵字中。本文通過實例介紹在Delphi的應用程序中如何對Windows 98的注冊表進行操作(如:創建關鍵字、獲取一個關鍵字的數據值、刪除關鍵字、刪除關鍵字中的數據值等),下列示例程序均在Delphi 4.0中調試通過,請放心使用。 Delphi中定義了一個Tregistry類,通過使用這個類中封裝的很多有關對注冊表操作的方法和屬性可以完成對注冊表的操作。 1、 在注冊表中創建一個新的關鍵字 Tregistry類中有一個CreateKey方法,使用該方法可以在注冊表中創建一個新的關鍵字,該方法的原型聲明為:function CreateKey(const Key: string) : Boolean; 示例代碼如下: unit passwd; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Registry; type Tpassword = class(TForm) Label1: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; implementation {$R *.DFM} procedure Tpassword.Button1Click(Sender: TObject); var MyReg : TRegistry; begin MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE; try if MyReg.OpenKey('\SOFTWARE\',FALSE) then if not MyReg.KeyExists('Passwd') then begin MyReg.CreateKey('Passwd'); if MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then Label1.Caption := '關鍵字Passwd已建立!' else Label1.Caption := '關鍵字Passwd無法建立!'; end else Label1.Caption := '關鍵字Passwd已經存在!' else Label1.Caption := '注冊表打不開!'; MyReg.CloseKey; finally MyReg.Free; end; end; end. 2、 向注冊表關鍵字中寫入相關的數據值 在Tregistry類中提供了一系列的Write方法用來寫入與當前關鍵字相關的數據值。常用方法的原型定義如下: procedure WriteString(const Name, Value : string); procedure WriteInteger(const Name : string ; Value : Integer); procedure WriteFloat(const Name : string ; Value : Double); procedure WriteTime(const Name : string ; Value : TDateTime); procedure WriteBool(const Name : string ; Value : Boolean); 示例代碼: procedure TForm1.Button1Click(Sender: TObject); var MyReg : TRegistry; begin MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE; try if not MyReg.OpenKey('\SOFTWARE\',FALSE) then ; if not MyReg.KeyExists('Passwd') then MyReg.CreateKey('Passwd'); if not MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then ; MyReg.WriteString('pwd1','mypassword1'); MyReg.WriteInteger('pd2',19642); MyReg.CloseKey; finally MyReg.Free; end; end; 3、 從注冊表關鍵字中讀出相關的數據值 在Tregistry類中還提供了與Write方法相對應用的用來讀出與當前關鍵字相關的數據值。常用方法的原型定義如下: founction ReadString(const Name : string) : string; founction ReadInteger(const Name : string) : Integer; founction ReadFloat(const Name : string) : Double; founction ReadTime(const Name : string) : TdateTime; founction ReadBool(const Name) : Boolean; 示例程序如下: procedure TForm1.Button1Click(Sender: TObject); var MyReg : TRegistry; begin MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE; try if not MyReg.OpenKey('\SOFTWARE\',FALSE) then ; if not MyReg.KeyExists('Passwd') then ; if not MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then ; Label1.Caption := MyReg.ReadString('pwd1'); Label2.Caption := IntToStr(MyReg.ReadInteger('pd2')); MyReg.CloseKey; finally MyReg.Free; end; end; 4、 從注冊表刪除關鍵字或指定的數據值 使用Tregistry中提供的DeleteKey和DeleteValue方法可以刪除指定的關鍵字和數據值。這兩個方法的原型定義如下: function DeleteKey(const Key : string) : Boolean; function DeleteValue(const Key : string) : Boolean; 使用DeleteKey方法刪除指定的關鍵字時,如果被刪除的關鍵字在任何層次有子關鍵字,它們將同時被刪除。上面兩個方法在執行時,如果刪除成功,則返回True;否則返回False。 示例程序如下: procedure TForm1.Button1Click(Sender: TObject); var MyReg : TRegistry; begin MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE; try if not MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then ; if MyReg.DeleteValue('pwd1') then label1.Caption := 'value [pwd1] has deleted!'; MyReg.CloseKey; if not MyReg.OpenKey('\SOFTWARE\',FALSE) then ; if MyReg.DeleteKey('Passwd') then label2.Caption := 'Key [passwd] has deleted!'; MyReg.CloseKey; finally MyReg.Free; end; end; 通過上面的實例我們簡單介紹了Delphi中對注冊表的相關操作。在Tregistry類中還有很多對注冊表的關鍵字和數據值的操作方法,但其基本的操作方法和上面的示例程序在同小異,限于篇幅就不贅述了。
|