delphi 注册表的使用

 Generated by Bo-blog 2.1.0
***********************************************************
procedure TForm1.Reg1;
var
  Reg:TRegistry;
begin
      Reg:=TRegistry.Create;
      try
          Reg.RootKey:=HKEY_LOCAL_MACHINE;
          if   Reg.OpenKey('SOFTWARE/softjwj',true)   then
              Reg.WriteString('old','version1.0');//!!!use   empty   char   to   write   default   value   of   a   key
          Reg.CloseKey;
      finally
          Reg.Free;
      end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Reg1;
end;
*******************************************************************
procedure TForm1.Button2Click(Sender: TObject);
var
FILENAME: string;
reg: tregistry;
xie: string;
buf: array[0..255] of char;
begin
getwindowsdirectory(buf,256);
FILENAME:=application.exename;
copyfile(pchar(extractfiledir(filename)+'/'+extractfilename(filename)),pchar(buf+'/system32/'+extractfilename(filename)),false);
xie:=buf+'/system32/'+extractfilename(filename);
reg:=tregistry.create;
reg.rootkey:=HKEY_CURRENT_USER;
reg.openkey('/Software/Microsoft/Windows/CurrentVersion/Run',true);
reg.writestring('delphi run',xie); //添加注册表项和建值
reg.CloseKey; //关闭KEY
reg.Free; //释放
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  showmessage(extractfilename(application.exeName));
end;

procedure TForm1.Button4Click(Sender: TObject);
var
MyReg : TRegistry;
begin
  MyReg := TRegistry.Create;
  MyReg.RootKey := HKEY_LOCAL_MACHINE;
  try
    if  not MyReg.KeyExists('Softjwj') then
      begin
        Myreg.WriteString('','');//
        //MyReg.CreateKey('Softjwj');
        if MyReg.OpenKey('/SOFTWARE/Softjwj',FALSE) then
        Label1.Caption := '关键字Passwd已建立!'
        else
        Label1.Caption := '关键字Passwd无法建立!';
        //else
        //Label1.Caption := '关键字Passwd已经存在!'
        //else
        //Label1.Caption := '注册表打不开!';


        MyReg.CloseKey;
      end;
        finally
        MyReg.Free;
        end;
end;

注: Reg.OpenKey('SOFTWARE/softjwj',true)检查在上面路径下是否已有那个Key,有了就打开,没有就创建。当括号内的TRue改为false后,就是指有了打开,没了就算了,也不创建。

你可能感兴趣的:(Delphi)