Solved

Using the registry to save options

Posted on 1998-12-22
9
200 Views
Last Modified: 2010-04-04
I am wruting an app, and I would like to know how to save information to the registry, that can be retrieved later so the user doesn't have to set the options they want every time they use the program.
0
Comment
Question by:pf2
9 Comments
 
LVL 4

Expert Comment

by:dwwang
Comment Utility
There is tregistry component in Delphi, see the help of it, I think you can get all.
0
 

Expert Comment

by:samone
Comment Utility
Put Registry under uses and you can look in the ShowForm down here to see how i solved it.
also type Readstring alt writestring and press F1 for help.

unit UpgrForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Registry;

  TFormUpgrade = class(TForm)
    OpenDialog1: TOpenDialog;
    ESource: TEdit;
    EDestination: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BSource: TButton;
    BDestination: TButton;
    Button3: TButton;
    procedure BSourceClick(Sender: TObject);
    procedure BDestinationClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure OpenDialog1SelectionChange(Sender: TObject);
    procedure ESourceChange(Sender: TObject);
    procedure EDestinationChange(Sender: TObject);
  private
    { Private declarations }
    VSource:String;
    VDestination:String;
    VCharsource:Pchar;
    VCharDestination:Pchar;
    VOpen:Byte;
    VIniFile: TRegIniFile; {Variabel för windowsregistret
  public
    { Public declarations }
  end;

var
  FormUpgrade: TFormUpgrade;

implementation

{$R *.DFM}

const
  SECTION = 'SAPost'; {Konstant för windowsregistret

procedure TFormUpgrade.BSourceClick(Sender: TObject);
begin
  VOpen:=1;
  opendialog1.Execute;
end;

procedure TFormUpgrade.BDestinationClick(Sender: TObject);
begin
  VOpen:=2;
  opendialog1.Execute;
end;

procedure TFormUpgrade.Button3Click(Sender: TObject);
begin
  VSource:=VIniFile.ReadString(SECTION, 'Source','');
  VDestination:=VIniFile.ReadString(SECTION, 'Destination','');
  VCharsource:=Pchar(VSource);
  VCharDestination:=Pchar(VDestination);
  if CopyFile(VCharsource,VCharDestination,False)=False then
  MessageDlg(''+#39+'Det gick inte att uppgradera'+#39+'',   mtInformation, [mbOK], 0);
end;

procedure TFormUpgrade.FormShow(Sender: TObject);
begin
  VIniFile := TReginifile.Create('Software\SAMONE');
  VSource:=VIniFile.ReadString(SECTION, 'Source','');
  VDestination:=VIniFile.ReadString(SECTION, 'Destination','');
  ESource.text:=VSource;
  EDestination.text:=VDestination;
end;

procedure TFormUpgrade.OpenDialog1SelectionChange(Sender: TObject);
begin
  If VOpen=1 then
  Begin
    VSource:=OpenDialog1.FileName;
    VIniFile.WriteString(SECTION, 'Source',VSource);
  end;
  If VOpen=2 then
  Begin
    VDestination:=OpenDialog1.FileName;
    VIniFile.WriteString(SECTION, 'Destination',VDestination);
  end;
  ESource.text:=VSource;
  EDestination.text:=VDestination;
end;

procedure TFormUpgrade.ESourceChange(Sender: TObject);
begin
   VIniFile.WriteString(SECTION, 'Source',ESource.text);
end;

procedure TFormUpgrade.EDestinationChange(Sender: TObject);
begin
   VIniFile.WriteString(SECTION, 'Destination',EDestination.text);
end;

end.
0
 

Author Comment

by:pf2
Comment Utility
Let me try and see if it works!   =o)
thank for helping
0
 

Author Comment

by:pf2
Comment Utility
I tried all possible ways of getting this to work, but it wouldn't.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:pf2
Comment Utility
I tried all possible ways of getting this to work, but it wouldn't.
0
 
LVL 1

Accepted Solution

by:
rpetruni earned 120 total points
Comment Utility
Hi pf2,
Samone gave you code that is OK, the only problem is that i did not explain what you really need of it, so here is what you need to do....
First if you wannt to write something in registry you have to know where, and how (you probably know what :-) )
You could use HKEY_CURRENT_USER\Software to create key with name of you program, or company, if you have more applications, to save and retreive data from...
To have acces to registry you will have to declare variable of TRegistry tipe, something like this:
var
    Registry: TRegistry;
then in you procedure, you will have to gain acces to registry, something like this:
begin
      Registry:=TRegistry.Create;
//Position your self in one of root keys    
      Registry.RootKey:=HKEY_LOCAL_MACHINE;
// if you use true, it will create key (if is not exist allready, if you use false it will not create it, yust read the value inside it...      Registry.OpenKey('Software\NameOfYourProgramKey\Keyname, true);
//Now you will want to write something in registry:
    Reg.WriteString('Something', SomeVariable);
//When you done whit writing to registry you have to release it...
    Reg.CloseKey;
//Now if you wannt to read this value, repeit first steps:
    Registry:=TRegistry.Create;
    Registry.RootKey:=HKEY_LOCAL_MACHINE;
Registry.OpenKey('Software\NameOfYourProgramKey\Keyname, false); // here is false, you only want to read the value...
//And finally we will read the value from it...
    SomeOtherVariable:=Reg.ReadString('KeyName')
//And release it...
    Reg.CloseKey;
Of corse you will have to put Registry in uses for this to work...
Similar to this is reading and writing in ini files...
If you have any questions .... :-)
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
What goes wrong? The code looks pretty good to me.

Cheers,
Raymond.
0
 

Author Comment

by:pf2
Comment Utility
Thank you very much, I tried it and it worked
Thanks to Samone and rpetruni!   =o)
0
 

Expert Comment

by:samone
Comment Utility
Sorry for not mention how too use it.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now