Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Default Value of Registry Key

Tags:

delphi

I am having one Delphi XE2 project to work something with registry key. So I have defined the following codes :

procedure TMainForm.BitBtn01Click(Sender: TObject);
var
  RegistryEntry: TRegistry;
begin
  RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
  RegistryEntry.RootKey:= HKEY_LOCAL_MACHINE;
  if (not RegistryEntry.KeyExists('Software\MyCompanyName\MyName\')) then
    begin
      RegistryEntry.Access:= KEY_WRITE or KEY_WOW64_64KEY;
      RegistryEntry.OpenKey('Software\MyCompanyName\MyName\',True);
      RegistryEntry.WriteString('', 'MyFirstProject');
    end
  else
    begin
      Memo01.Lines.Add(RegistryEntry.ReadString('(Default)')); //Not Working
      Memo01.Lines.Add(RegistryEntry.ReadString('')); //Not Working
      Memo01.Lines.Add(RegistryEntry.ReadString('@')); //Not Working
    end;
  RegistryEntry.CloseKey();
  RegistryEntry.Free;
end;

My requirement is to check the Default Value of "MyName" and to show in Memo01. But nothing is heppening. So I have tried another way as follows :

procedure TMainForm.BitBtn01Click(Sender: TObject);
var
  RegistryEntry: TRegistry;
  RegistryString: string;
begin
  RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
  RegistryEntry.RootKey:= HKEY_LOCAL_MACHINE;
  if (not RegistryEntry.KeyExists('Software\MyCompanyName\MyName\')) then
    begin
      RegistryEntry.Access:= KEY_WRITE or KEY_WOW64_64KEY;
      RegistryEntry.OpenKey('Software\MyCompanyName\MyName\',True);
      RegistryEntry.WriteString('', 'MyFirstProject');
    end
  else
    begin
      RegistryString := RegistryEntry.ReadString('(Default)'); //Not Working
      RegistryString := RegistryEntry.ReadString(''); //Not Working
      RegistryString := RegistryEntry.ReadString('@'); //Not Working
      Memo01.Lines.Add(RegistryString);
    end;
  RegistryEntry.CloseKey();
  RegistryEntry.Free;
end;

But it is also not working.

like image 982
Rubi Halder Avatar asked Apr 23 '13 18:04

Rubi Halder


1 Answers

There is no Openkey in your else part.

Const
  C_KEY='Software\MyCompanyName\MyName\';
var
  RegistryEntry: TRegistry;
  RegistryString: string;
begin
  RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
  try
    RegistryEntry.RootKey := HKEY_LOCAL_MACHINE;
    if (not RegistryEntry.KeyExists(C_KEY)) then
    begin
      RegistryEntry.Access := KEY_WRITE or KEY_WOW64_64KEY;
      if RegistryEntry.OpenKey(C_KEY, true) then
        RegistryEntry.WriteString('', 'MyFirstProject');
    end
    else
    begin
      RegistryEntry.Access := KEY_READ or KEY_WOW64_64KEY;
      if RegistryEntry.OpenKey(C_KEY, false) then
      begin
        Memo01.Lines.Add(RegistryEntry.ReadString(''));
      end;
    end;
    RegistryEntry.CloseKey();
  finally
    RegistryEntry.Free;
  end;
end;
like image 192
bummi Avatar answered Nov 08 '22 12:11

bummi