想实现下面操作:
如果发现HKEY_USERS\.DEFAULT\rsddd存在就读取它的数值,如果不存在就建立该键,怎么写代码呢?

解决方案 »

  1.   

    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;