2
如何從一個InnoSetup(Pascal)腳本中「接觸」一個文件,即將其最後修改時間更新爲當前時間?如何從InnoSetup腳本中「觸摸」文件?
如何從一個InnoSetup(Pascal)腳本中「接觸」一個文件,即將其最後修改時間更新爲當前時間?如何從InnoSetup腳本中「觸摸」文件?
下面是該TouchFile函數的代碼片段:
[Code]
function CreateFile(
lpFileName : String;
dwDesiredAccess : Cardinal;
dwShareMode : Cardinal;
lpSecurityAttributes : Cardinal;
dwCreationDisposition : Cardinal;
dwFlagsAndAttributes : Cardinal;
hTemplateFile : Integer
): THandle;
#ifdef UNICODE
external '[email protected] stdcall';
#else
external '[email protected] stdcall';
#endif
procedure GetSystemTimeAsFileTime(var lpSystemTimeAsFileTime: TFileTime);
external '[email protected]';
function SetFileModifyTime(hFile:THandle; CreationTimeNil:Cardinal; LastAccessTimeNil:Cardinal; LastWriteTime:TFileTime): BOOL;
external '[email protected]';
function CloseHandle(hHandle: THandle): BOOL;
external '[email protected] stdcall';
function TouchFile(FileName: String): Boolean;
const
{ Win32 constants }
GENERIC_WRITE = $40000000;
OPEN_EXISTING = 3;
INVALID_HANDLE_VALUE = -1;
var
FileTime: TFileTime;
FileHandle: THandle;
begin
Result := False;
FileHandle := CreateFile(FileName, GENERIC_WRITE, 0, 0, OPEN_EXISTING, $80, 0);
if FileHandle <> INVALID_HANDLE_VALUE then
try
GetSystemTimeAsFileTime(FileTime);
Result := SetFileModifyTime(FileHandle, 0, 0, FileTime);
finally
CloseHandle(FileHandle);
end;
end;
您好,我[`是looking`(http://stackoverflow.com/a/10163139/960757)的`CreateFile` InnoSetup原型,我發現你的代碼並優化它一點點。另外,請注意,此代碼適用於InnoSetup的ANSI版本。如果你需要將它用於Unicode版本,你應該將`CreateFile`函數的import定義爲`CreateFileW`而不是`CreateFileA`或者使用[`kobik`]建議的技巧(http://stackoverflow.com/users/ 937125/kobik)在這[`post`](http://stackoverflow.com/a/9670505/960757)。無論如何分享ide的+1 ;-) – TLama 2012-04-16 03:31:51