Solved

Using the registry to save options

Posted on 1998-12-22
9
205 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 60
code issue 8 131
error 1.1 400 Bad request idhttp delphi 18 90
QRReport  TQrmemo vertical stretching 1 46
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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