//..............................................................................
uses kısmına "Registry, IniFiles" eklemeyi unutmayın...
//..............................................................................
Örnek-1: Tek bir registry anahtarı okuyacaksanız;
const
SettingsRoot = HKEY_LOCAL_MACHINE;
SettingsKey = '\Software\Delphi Turk\Codebank';
var
Version: String;
begin
Version := RegRead(nil, SettingsRoot, SettingsKey, 'Version', '3.1.0.0', rwtString);
end;
//..............................................................................
Örnek-2: Birden fazla registry anahtarı okuyacaksanız;
const
SettingsRoot = HKEY_LOCAL_MACHINE;
SettingsKey = '\SOFTWARE\Microsoft\Windows\CurrentVersion';
var
Reg: TRegistry;
CommonFilesDir,
DevicePath,
MediaPath,
WallPaperDir: String;
begin
Reg := TRegistry.Create;
try
CommonFilesDir := RegRead(nil, SettingsRoot, SettingsKey, 'CommonFilesDir', '', rwtString);
DevicePath := RegRead(nil, SettingsRoot, SettingsKey, 'DevicePath', '', rwtString);
MediaPath := RegRead(nil, SettingsRoot, SettingsKey, 'MediaPath', '', rwtString);
WallPaperDir := RegRead(nil, SettingsRoot, SettingsKey, 'WallPaperDir', '', rwtString);
finally
Reg.Free;
end;
end;
//..............................................................................
FONKSİYON VE PROSEDÜR BİLDİRİMLERİ
//..............................................................................
type
TReadWriteType = (rwtString, rwtInteger, rwtBoolean);
procedure RegWrite(Reg: TRegistry; Root: DWORD; Key, Value: String;
Entry: Variant; WriteType: TReadWriteType; CreateIfNotExists:
Boolean = True);
function RegRead(Reg: TRegistry; Root: DWORD; Key, Value: String;
DefaultEntry: Variant; ReadType: TReadWriteType; CreateIfNotExists:
Boolean = True): Variant;
procedure IniWrite(Ini: TIniFile; FileName, Section, Ident: String; Value: Variant;
WriteType: TReadWriteType; CreateIfNotExists: Boolean = True);
function IniRead(Ini: TIniFile; FileName, Section, Ident: String; DefaultValue: Variant;
ReadType: TReadWriteType; CreateIfNotExists: Boolean = True): Variant;
//..............................................................................
FONKSİYON VE PROSEDÜR KOD LİSTESİ
//..............................................................................
procedure RegWrite(Reg: TRegistry; Root: DWORD; Key, Value: String;
Entry: Variant; WriteType: TReadWriteType; CreateIfNotExists: Boolean);
var
FreeOnExit: Boolean;
begin
FreeOnExit := Reg = nil;
if FreeOnExit then
Reg := TRegistry.Create;
try
Reg.RootKey := Root;
if Reg.OpenKey(Key, CreateIfNotExists) then
begin
case WriteType of
rwtString : Reg.WriteString(Value, Entry);
rwtInteger : Reg.WriteInteger(Value, Entry);
rwtBoolean : Reg.WriteBool(Value, Entry);
end;
end;
finally
if FreeOnExit then
Reg.Free;
end;
end;
//..............................................................................
function RegRead(Reg: TRegistry; Root: DWORD; Key, Value: String;
DefaultEntry: Variant; ReadType: TReadWriteType; CreateIfNotExists:
Boolean): Variant;
var
FreeOnExit: Boolean;
begin
FreeOnExit := Reg = nil;
if FreeOnExit then
Reg := TRegistry.Create;
try
Reg.RootKey := Root;
if Reg.OpenKey(Key, CreateIfNotExists) then
begin
if not Reg.ValueExists(Value) then
begin
RegWrite(nil, Root, Key, Value, DefaultEntry, ReadType);
Result := DefaultEntry;
end
else case ReadType of
rwtString : Result := Reg.ReadString(Value);
rwtInteger : Result := Reg.ReadInteger(Value);
rwtBoolean : Result := Reg.ReadBool(Value);
end;
end
finally
if FreeOnExit then
Reg.Free;
end;
end;
//..............................................................................
procedure IniWrite(Ini: TIniFile; FileName, Section, Ident: String; Value: Variant;
WriteType: TReadWriteType; CreateIfNotExists: Boolean);
var
FreeOnExit: Boolean;
begin
FreeOnExit := Ini = nil;
if FreeOnExit then
Ini := TIniFile.Create(FileName);
try
case WriteType of
rwtString : Ini.WriteString(Section, Ident, Value);
rwtInteger : Ini.WriteInteger(Section, Ident, Value);
rwtBoolean : Ini.WriteBool(Section, Ident, Value);
end;
finally
if FreeOnExit then
Ini.Free;
end;
end;
//..............................................................................
function IniRead(Ini: TIniFile; FileName, Section, Ident: String; DefaultValue: Variant;
ReadType: TReadWriteType; CreateIfNotExists: Boolean): Variant;
var
FreeOnExit: Boolean;
begin
FreeOnExit := Ini = nil;
if FreeOnExit then
Ini := TIniFile.Create(FileName);
try
if not (ini.SectionExists(Section) and ini.ValueExists(Section, Ident)) then
begin
IniWrite(nil, FileName, Section, Ident, DefaultValue, ReadType);
Result := DefaultValue;
end
else case ReadType of
rwtString : Result := Ini.ReadString(Section, Ident, DefaultValue);
rwtInteger : Result := Ini.ReadInteger(Section, Ident, DefaultValue);
rwtBoolean : Result := Ini.ReadBool(Section, Ident, DefaultValue);
end;
finally
if FreeOnExit then
Ini.Free;
end;
end;
//..............................................................................