Hi Andidreas,
ich benutze zum Speichern von Optionen immer
XML-Dateien.
Hier mal meinen Aufbau der Klasse:
Delphi-Quellcode:
unit uoptions;
interface
uses
XMLIntf, XMLDoc, SysUtils;
type
TOptions =
class(TObject)
private
FXMLDoc: IXMLDocument;
FLanguage:
String;
FFileName:
String;
public
constructor Create(
const OptionsFileName:
String);
destructor Destroy;
override;
procedure Load;
procedure Save(SaveInFile: Boolean = false);
// Optionen
property Language:
String read FLanguage
write FLanguage;
end;
implementation
{ TOptions }
constructor TOptions.Create(
const OptionsFileName:
String);
begin
FFileName := OptionsFileName;
FXMLDoc := TXMLDocument.Create(FFileName);
FXMLDoc.LoadFromFile(FFileName);
if (
not (FXMLDoc.DocumentElement.NodeName = '
settings'))
then
begin
raise Exception.Create('
Not a valid settingsfile');
end;
FXMLDoc.Active := true;
end;
destructor TOptions.Destroy;
begin
FXMLDoc.SaveToFile(FFileName);
FXMLDoc.Active := false;
inherited;
end;
procedure TOptions.Load;
function DoLoad(NodeName:
String;
Default:
String = '
'):
String;
var
Node: IXMLNode;
begin
Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(NodeName);
if Node <>
nil then
Result := Node.Text
else
Result :=
Default;
end;
begin
// Laden
Language := DoLoad('
language', '
de');
end;
procedure TOptions.Save(SaveInFile: Boolean = false);
procedure DoSave(NodeName:
String; Text:
String);
var
Node: IXMLNode;
begin
Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(NodeName);
if Node =
nil then
Node := FXMLDoc.DocumentElement.AddChild(NodeName);
Node.Text := Text;
end;
begin
// Speichern
DoSave('
language', Language);
if SaveInFile
then
FXMLDoc.SaveToFile(FFilename);
end;
end.
Das ist jetzt nicht das nonplusultra, aber vielleicht ein kleiner Denkanstoß.