본문 바로가기

Program/Delphi

[Delphi] 레지스트리(Registry) 등록

레지스트리 등록하는 간단한 예제 소스


1. 대충 폼을 그리고



2. 소스

   (1) 'TRegistry' Class를 이용하여 myRegistry 선언.

   (2) myRegistry 의 Rootkey 설정.

   (3) 사용할 경로의 레지스트리  존재 유무 확인 후 레지스트리 등록


   * 추가적으로 'TRegIniFile' Class를 이용하여 ini파일 읽기/쓰기 식으로도 등록 가능하다.


unit frmRegiSetup;


interface


uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, Registry;


type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


uses testRegCommonConst;


{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);

var

   myRegistry : TRegistry;

   regData1, regData2, regData3 : string;

   temp_regData1, temp_regData2, temp_regData3 : string;

begin

   regData1 := 'regData1';

   regData2 := 'regData2';

   regData3 := 'regData3';


   myRegistry := TRegistry.Create;


   myRegistry.RootKey := HKEY_CURRENT_USER;


   if not myRegistry.KeyExists(TEST_Registry_REGPATH)then

   begin

      myRegistry.CreateKey(TEST_Registry_REGPATH);


      if myRegistry.OpenKey(TEST_Registry_REGPATH, False) then

      begin

         temp_regData1 := myRegistry.ReadString(TEST_Registry_regData1);

         temp_regData2 := myRegistry.ReadString(TEST_Registry_regData2);

         temp_regData3 := myRegistry.ReadString(TEST_Registry_regData3);


         if (temp_regData1 = '') or (temp_regData2 = '') or (temp_regData3 = '') then

         begin

            myRegistry.RootKey := HKEY_CURRENT_USER;


            myRegistry.WriteString('regData1', regData1 + #0 );

            myRegistry.WriteString('regData2', regData2 + #0 );

            myRegistry.WriteString('regData3', regData3 + #0 );

         end;


         temp_regData1 := myRegistry.ReadString(TEST_Registry_regData1);

         temp_regData2 := myRegistry.ReadString(TEST_Registry_regData2);

         temp_regData3 := myRegistry.ReadString(TEST_Registry_regData3);


         if (temp_regData1 = regData1) and (temp_regData2 = regData2) and (temp_regData3 = regData3) then

         begin

            ShowMessage('  레지스트리 등록완료.  ');

         end;

      end;

   end

   else

   begin

      ShowMessage('  이미 등록 되어 있습니다...  ');

   end;


   myRegistry.Free;

end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

   Action := caFree;

end;


procedure TForm1.Button2Click(Sender: TObject);

begin

   close;

end;


end.


testRegCommonConst.pas

unit testRegCommonConst;


interface


const

  //레지스트리 정보

  TEST_Registry_REGPATH = '\SOFTWARE\Program path1\Program path2';


  TEST_Registry_regData1 = 'regData1';

  TEST_Registry_regData2 = 'regData2';

  TEST_Registry_regData3 = 'regData3';


implementation


end.


3. 실행 후 확인

윈도우 실행창에 'regedit' 실행 후 확인