Solved

Using the registry to save options

Posted on 1998-12-22
9
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pf2
ID: 1352576
I tried all possible ways of getting this to work, but it wouldn't.
0
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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 Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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