mantra246
asked on
Registry question
Anyone have a good idea on what the best way to save/load information from a record structure to the registry?
record structure:
Type
TidList = packed record
item1 : boolean;
item2 : byte;
itype : string [255];
iavail : boolean;
end
record structure:
Type
TidList = packed record
item1 : boolean;
item2 : byte;
itype : string [255];
iavail : boolean;
end
Here's an example, all-in-one, break out into separate routines...
procedure SaveOrLoad(aItem:tIDList);
const
KeyName='SAVEME';
var
R:tregistry;
begin
R:=TRegistry.Create(KEY_RE AD);
try
R.RootKey = HKEY_LOCAL_MACHINE;
// save
R.OpenKey(KeyName,True); //create if doesn't exist
with aItem do begin
R.WriteBool('ITEM1');
R.WriteString('ITYPE');
R.WriteInteger('ITEM2');
R.WriteBool('IAVAIL');
end;
// Load
R.OpenKey(KeyName,False); //do not create if doesn't exist
with aItem do begin
item1:=R.ReadBool('ITEM1') ;
itype:=R.ReadString('ITYPE ');
item2:=R.ReadInteger('ITEM 2');
iavail:=R.ReadBool('IAVAIL ');
end;
finally
R.Free;
end;
end;
procedure SaveOrLoad(aItem:tIDList);
const
KeyName='SAVEME';
var
R:tregistry;
begin
R:=TRegistry.Create(KEY_RE
try
R.RootKey = HKEY_LOCAL_MACHINE;
// save
R.OpenKey(KeyName,True); //create if doesn't exist
with aItem do begin
R.WriteBool('ITEM1');
R.WriteString('ITYPE');
R.WriteInteger('ITEM2');
R.WriteBool('IAVAIL');
end;
// Load
R.OpenKey(KeyName,False); //do not create if doesn't exist
with aItem do begin
item1:=R.ReadBool('ITEM1')
itype:=R.ReadString('ITYPE
item2:=R.ReadInteger('ITEM
iavail:=R.ReadBool('IAVAIL
end;
finally
R.Free;
end;
end;
Oops...this is the SAVE part:
with aItem do begin
R.WriteBool('ITEM1',item1) ;
R.WriteString('ITYPE',ityp e);
R.WriteInteger('ITEM2',ite m2);
R.WriteBool('IAVAIL',iavai l);
end;
with aItem do begin
R.WriteBool('ITEM1',item1)
R.WriteString('ITYPE',ityp
R.WriteInteger('ITEM2',ite
R.WriteBool('IAVAIL',iavai
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
rbohac, agreed that you CAN do that however I recommend against it; by writing the individual elements, you can survive a change of structure later on, e.g., allowing the use of registry entries from a prior release of the software even though the internal representation might have changed.
Another consideration - Microsoft is moving away from a centralized registry in .NET, in favour of local INI files. Therefore I believe that TRegistryIniFile is a better choice since it supports both the registry and *.ini files, depending on the opearting system.
ASKER
sftweng - neat method you use, but what do you mean about ReadSection? (i've never really used the registry before other than to store the odd variable or two)
ASKER
> Microsoft is moving away from a centralized registry in .NET, in favour of local INI files
I wish Microsoft would make up their mind - can you say U-Turn. It used to be that INI files were the expected way to go then the registry was the better option to be used - now ini files are to be used again! Sheesh!
I wish Microsoft would make up their mind - can you say U-Turn. It used to be that INI files were the expected way to go then the registry was the better option to be used - now ini files are to be used again! Sheesh!
I agree, but it is useful if you don't want the data to be <easily> editably by the end user
mantra246, ReadSection from Delphi help:
Reads all the key names from a specified section of an INI file into a string list.
procedure ReadSection (const Section: String; Strings: TStrings); override;
Description
Call ReadSection to retrieve the names of all keys within a specified section of an INI file into a string list object.
Section identifies the section from which to retrieve a list of key names. Strings specifies the string object to hold the retrieved names. Strings can point to a TString object such as a string list, or to a component property, such as Items for a TListBox component, that is a TString object itself.
Reads all the key names from a specified section of an INI file into a string list.
procedure ReadSection (const Section: String; Strings: TStrings); override;
Description
Call ReadSection to retrieve the names of all keys within a specified section of an INI file into a string list object.
Section identifies the section from which to retrieve a list of key names. Strings specifies the string object to hold the retrieved names. Strings can point to a TString object such as a string list, or to a component property, such as Items for a TListBox component, that is a TString object itself.
rbohac, a good point. The usual tradeoff between ease of use and security. ;-)
ASKER
Thanks to both sftweng and rbohac for showing two very good methods.
I raised the points to give you both the original 250 points each.
I raised the points to give you both the original 250 points each.
Thanks.
type
MyString = string[255];
TidList = class
private
public
item1 : boolean;
item2 : byte;
itype : MyString;
iavail : boolean;
procedure SetValues (it1 : Boolean; it2 : Byte; itype : MyString; iav : Boolean);
procedure LoadFromRegistry;
procedure SaveToRegistry;
end;