Programming C# C++ (7) Delphi (617) .NET (2) Database (71) Delphi IDE (89) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280) Java (8) JavaScript (31) perl (9) php (4) VBScript (1) Visual Basic (1)
Exchange Links About this site Links to us
New related comments Number of comments in the last 48 hoursThe meaning of Runtime Error 217 1 new comments
|
Best way to store a TDateTime in the registry
This article has not been rated yet. After reading, feel free to leave comments and rate it.
Question:
What is the best/most commonly used format for storing TDateTime in the reguistry ? Converting it to a string and then back is not always satisfactory as people can change their date and time formats and we do not want to impose any restrictions on the software.
Answer:
TDateTime is a float, so you can store it as a binary value. See the sample code below.
Download file DateRegistry.dpr.
 | |  | | program DateRegistry;
uses
Windows,
Dialogs,
Registry,
SysUtils;
procedure SaveDate(const sKey: string; const sField: string; aDate: TDateTime);
begin
with TRegistry.Create do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey(sKey, True) then
begin
WriteBinaryData(sField, aDate, SizeOf(aDate));
CloseKey;
end;
Free;
end;
end;
function ReadDate(const sKey: string; const sField: string) : TDateTime;
begin
Result := 0;
with TRegistry.Create do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey(sKey, False) then
begin
try
ReadBinaryData(sField, Result, SizeOf(Result));
except
end;
CloseKey;
end;
Free;
end;
end;
var
dDate: TDateTime;
begin
SaveDate('\Software\preview\Demo', 'LastDate', Now);
dDate := ReadDate('\Software\preview\Demo', 'LastDate');
ShowMessage(DateTimeToStr(dDate));
end. | |  | |  |
Comments:
|
anonymous from China
|
|
|
commonly
|
|