2020年7月28日 星期二

TRegistry 操作

Uses Registry;

//取得RootKey下的節點
procedure TForm1.Button1Click(Sender: TObject);
var reg: TRegistry;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.OpenKey('', False);
  reg.GetKeyNames(memo1.Lines); //取得RootKey下的節點
  reg.CloseKey;
  FreeandNil(reg);
end;

 
//取得節點內的設定
procedure TForm1.Button1Click(Sender: TObject);
var reg: TRegistry;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.OpenKey('SOFTWARE\Microsoft\Jet\4.0\Engines\Excel', False);
  reg.GetValueNames(memo1.Lines); //取得節點內的設定
  reg.CloseKey;
  FreeandNil(reg);
end;


//取值
procedure TForm1.Button2Click(Sender: TObject);
var reg: TRegistry;
  sValue:Variant;
  sName:String;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.OpenKey('SOFTWARE\Microsoft\Jet\4.0\Engines\Excel', False);   //
  sName := 'TypeGuessRows';
  if reg.ValueExists(sName) then
  begin
    //取節點內的設定值, 必須與設定值的資料型態相同
    if reg.GetDataType(sName)=rdInteger then
      sValue := reg.ReadInteger(sName)
    else if reg.GetDataType(sName)=rdString then
      sValue := reg.ReadString(sName);
  end;
  memo1.text := VarToStr(sValue);
  reg.CloseKey;
  FreeandNil(reg);
end;


//設定值
procedure TForm1.Button3Click(Sender: TObject);
var reg: TRegistry;
  sValue:Variant;
  sName:String;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.OpenKey('SOFTWARE\Microsoft\Jet\4.0\Engines\Excel', False);
  sName := 'TypeGuessRows';
  if reg.ValueExists(sName) then
  begin
    //取節點內的設定值, 必須與設定值的資料型態相同
    if reg.GetDataType(sName)=rdInteger then
      reg.WriteInteger(sName, 0)
    else if reg.GetDataType(sName)=rdString then
      reg.WriteInteger(sName, 0)
  end;
  reg.CloseKey;
  FreeandNil(reg);
end;

沒有留言:

張貼留言