Friday, January 14, 2011

how to read and store string values from or to the registry

uses
  Registry;

function TForm1.RegRead(Mykey, MyField: string): string;
var
  Reg: TRegistry;
begin
  //Create the Object
  Reg := TRegistry.Create;
  with Reg do
  begin
    //Sets the destination for our requests
    RootKey := HKEY_LOCAL_MACHINE;
    //Check if whe can open our key, if the key dosn't exist, we create it
    if OpenKey(MyKey, True) then
    begin
      //Is our field availbe
      if ValueExists(MyField) then
        //Read the value from the field
        Result := Readstring(MyField)
      else
        ShowMessage(MyField + ' does not exists under ' + MyKey);
    end
    else
      //There is a big error if we get an errormessage by
      //opening/creating the key
      ShowMessage('Error opening/creating key : ' + MyKey);
    CloseKey;
  end;
end;


procedure TForm1.RegWrite(Mykey, MyField, MyValue: string);
var
  Reg: TRegistry;
begin
  //Create the Object
  Reg := TRegistry.Create;
  with Reg do
  begin
    //Sets the destination for our requests
    RootKey := HKEY_LOCAL_MACHINE;
    //Check if we can open our key, if the key doesn't exist, we create it
    if OpenKey(MyKey, True) then
      //We don't need to check if the field is available because the
      //field is created by writing the value
      WriteString(MyField, MyValue)
    else
      //There is a big error if we gets an errormessage by
      //opening/creating the key
      ShowMessage('Error opening/creating key : ' + MyKey);
    CloseKey;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RegWrite('SOFTWARE\MyProgramm\', 'Path', 'C:\test.exe');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  label1.Caption := RegRead('SOFTWARE\MyProgramm', 'Path');
end;

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum