注册表的建立,写入,读取,删除

2017-01-04 19:18:26来源:CSDN作者:u012853614人点击

第七城市
unit Unit2;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Registry,  Dialogs, Buttons, StdCtrls;type  TForm2 = class(TForm)    SpeedButton1: TSpeedButton;    SpeedButton2: TSpeedButton;    SpeedButton3: TSpeedButton;    ListBox1: TListBox;    procedure FormClick(Sender: TObject);    procedure SpeedButton1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure SpeedButton2Click(Sender: TObject);    procedure SpeedButton3Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  Form2: TForm2;implementation{$R *.dfm}{*创建注册表*}procedure TForm2.FormClick(Sender: TObject);var  MyReg: TRegistry;       //声明一个注册表对象begin  try       //尝试执行    MyReg := TRegistry.Create;   //创建注册表对象    MyReg.RootKey := HKEY_CURRENT_USER;   //在该目录下创建注册表项    MyReg.OpenKey('/MyReg', True);   //创建注册表项为MyReg    ShowMessage('Create Success');  finally      //无论如何都强制执行    MyReg.CloseKey;   //关闭    MyReg.Free;        //释放内存  end;end;{*窗体初始化*}procedure TForm2.FormCreate(Sender: TObject);begin     SpeedButton1.Caption := 'Delete Myreg';end;{*删除注册表*}procedure TForm2.SpeedButton1Click(Sender: TObject);var  MyReg: TRegistry;begin  try  MyReg := TRegistry.Create;  MyReg.RootKey := HKEY_CURRENT_USER; //删除该目录下指定的注册表项  MyReg.DeleteKey('/MyReg');  ShowMessage('Delete Success');  finally  MyReg.CloseKey;  MyReg.Free;  end;end;{*向注册表写入信息*}procedure TForm2.SpeedButton2Click(Sender: TObject);var  MyReg: TRegistry;begin  try    MyReg := TRegistry.Create;    MyReg.RootKey := HKEY_CURRENT_USER;    if MyReg.OpenKey('/MyReg', True) then    begin      ShowMessage('Open success');      //ShowMessage调用CreateMessageDialog消息框      MyReg.WriteFloat('Float Style', 19.84);      MyReg.WriteString('String Style', 'Delphi');      MyReg.WriteBool('Boolean Style', True);      MyReg.WriteDateTime('DateTime Style', now);      MyReg.WriteInteger('Integer Style', 18);      Application.MessageBox('向注册表中写入数据成功!', '提示信息', 65);      //application.messagebox调用的是系统的弹窗消息框      MyReg.CloseKey;      MyReg.Free;    end;  Except    Application.MessageBox('向注册表中写入数据失败!', '提示信息', 64)  end;end;procedure TForm2.SpeedButton3Click(Sender: TObject);var  MyReg : TRegistry;begin  try    MyReg := TRegistry.Create;    MyReg.RootKey := HKEY_CURRENT_USER;    if MyReg.OpenKey('/MyReg', True) then    begin      ListBox1.Items.Add('Float Class: ' + FloatToStr(MyReg.ReadFloat('Float Style')));      ListBox1.Items.Add('String Class: ' + MyReg.ReadString('String Style'));      ListBox1.Items.Add('Boolean Class: ' + BoolToStr(MyReg.ReadBool('Boolean Style')));      ListBox1.Items.Add('DateTime Class: ' + DateTimeToStr(MyReg.ReadDateTime('DateTime Style')));      ListBox1.Items.Add('Integer Class: ' + IntToStr(MyReg.ReadInteger('Integer Style')));      Application.MessageBox('读取数据成功!', '提示信息', 65);    end;  except    Application.MessageBox('读取数据失败!', '提示信息', 65);  end;end;end.

第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台