Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using the registry to save options

Posted on 1998-12-22
9
Medium Priority
?
213 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 480 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

636 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