我有一個實用程序比較源文件和目標文件的日期/時間。這在大多數情況下都適用,但在比較不同時區中文件的日期/時間時會失敗。所以我需要一個UTC日期時間例程。UTC支持比較2個文件日期/時間
從環視看來,Windows API GetFileTime將支持這一點。我也發現這個包裝程序,但它在Delphi 2010下編譯時在GetFileTime調用上崩潰。
任何想法?或者是否有人有處理UTC時區等代碼片段,我可以傳遞2個文件名,這將告訴我,如果他們不同?
function CompareFileTimes(File1, File2 : String) : LongInt;
var
F1, F2 : THandle;
F1_CreateTime,
F1_LastAccess,
F1_LastWrite,
F2_CreateTime,
F2_LastAccess,
F2_LastWrite : PFileTime;
begin
//Initialize all variables
F1 := 0;
F2 := 0;
//Since these are pointers, we have to
//allocate memory for the FileTime structures
GetMem(F1_CreateTime, SizeOf(TFileTime));
GetMem(F1_LastAccess, SizeOf(TFileTime));
GetMem(F1_LastWrite, SizeOf(TFileTime));
GetMem(F2_CreateTime, SizeOf(TFileTime));
GetMem(F2_LastAccess, SizeOf(TFileTime));
GetMem(F2_LastWrite, SizeOf(TFileTime));
//Fill the structures with nulls for now
FillChar(F1_CreateTime, SizeOf(TFileTime), #0);
FillChar(F1_LastAccess, SizeOf(TFileTime), #0);
FillChar(F1_LastWrite, SizeOf(TFileTime), #0);
FillChar(F2_CreateTime, SizeOf(TFileTime), #0);
FillChar(F2_LastAccess, SizeOf(TFileTime), #0);
FillChar(F2_LastWrite, SizeOf(TFileTime), #0);
//Get file handles for the files in question
//Notice that even though we're using CreateFile
//the open disposition for the file is OPEN_EXISTING
F1 := CreateFile(PChar(F1), 0, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
F2 := CreateFile(PChar(F2), 0, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
//Get the file times for the files.
GetFileTime(F1, F1_CreateTime, F1_LastAccess, F1_LastWrite);
GetFileTime(F2, F2_CreateTime, F2_LastAccess, F2_LastWrite);
//Assign the function's result to comparison
//-1, File1 is younger than File2
//0, File1 is the same as File2
//+1 File1 is older than File2
Result := CompareFileTime(F1_CreateTime^, F2_CreateTime^);
//Free the memory allocated to the pointers
FreeMem(F1_CreateTime, SizeOf(TFileTime));
FreeMem(F1_LastAccess, SizeOf(TFileTime));
FreeMem(F1_LastWrite, SizeOf(TFileTime));
FreeMem(F2_CreateTime, SizeOf(TFileTime));
FreeMem(F2_LastAccess, SizeOf(TFileTime));
FreeMem(F2_LastWrite, SizeOf(TFileTime));
end;
任何幫助將不勝感激。
提示:聲明TFileTime變量代替PFileTime變量,你不必GetMem和FreeMem(但當然FillChar)。 – 2010-08-16 04:51:58
將聲明更改爲TFileTime,然後在CreateFile調用上出現編譯錯誤,因爲它期待PFileTime。 – TallGuy 2010-08-16 05:26:26
@TallGuy:使用地址運算符「@」來傳入指針,例如'GetFileTime(F1,@ F1_CreateTime,@ F1_LastAccess,@ F1_LastWrite);' – 2010-08-16 05:45:32