swd',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: TObje
ct);
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类中还有很多对注册表的关键字和数据值的操作方法,但其基本的操作方法和上面的示例程序大同小异,限于篇幅就不赘述了。