Mega Code Archive

 
Categories / Delphi / Strings
 

How to read and store string values fromto the Registry

Title: How to read and store string-values from/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;