Solved

Using the registry to save options

Posted on 1998-12-22
9
204 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
ID: 1352573
There is tregistry component in Delphi, see the help of it, I think you can get all.
0
 

Expert Comment

by:samone
ID: 1352574
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
ID: 1352575
Let me try and see if it works!   =o)
thank for helping
0
 

Author Comment

by:pf2
ID: 1352576
I tried all possible ways of getting this to work, but it wouldn't.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pf2
ID: 1352577
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
ID: 1352578
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
ID: 1352579
What goes wrong? The code looks pretty good to me.

Cheers,
Raymond.
0
 

Author Comment

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

Expert Comment

by:samone
ID: 1352581
Sorry for not mention how too use it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

16 Experts available now in Live!

Get 1:1 Help Now