Get a files last updated time using pascal (innosetup)

前端 未结 2 1879
鱼传尺愫
鱼传尺愫 2021-01-25 13:33

In the uninstall portion of an innosetup script, I\'d like to add a check to see if a specific file\'s last update datetime occured within the last 10 mins.

Does anyone

2条回答
  •  逝去的感伤
    2021-01-25 14:22

    You can use the Windows API function GetFileAttributesEx to get the last modification date. Putting this in your [CODE] section should work:

    const
        GetFileExInfoStandard = $0;
    
    type 
        FILETIME = record 
          LowDateTime:  DWORD; 
          HighDateTime: DWORD; 
        end; 
    
        WIN32_FILE_ATTRIBUTE_DATA = record 
          FileAttributes: DWORD; 
          CreationTime:   FILETIME; 
          LastAccessTime: FILETIME; 
          LastWriteTime:  FILETIME; 
          FileSizeHigh:   DWORD; 
          FileSizeLow:    DWORD; 
        end; 
    
        SYSTEMTIME = record 
          Year:         WORD; 
          Month:        WORD; 
          DayOfWeek:    WORD; 
          Day:          WORD; 
          Hour:         WORD; 
          Minute:       WORD; 
          Second:       WORD; 
          Milliseconds: WORD; 
        end; 
    
    function GetFileAttributesEx (
        FileName:            string;  
        InfoLevelId:         DWORD; 
        var FileInformation: WIN32_FILE_ATTRIBUTE_DATA
        ): Boolean; 
    external 'GetFileAttributesExA@kernel32.dll stdcall'; 
    
    function FileTimeToSystemTime(
        FileTime:        FILETIME; 
        var SystemTime:  SYSTEMTIME
        ): Boolean; 
    external 'FileTimeToSystemTime@kernel32.dll stdcall'; 
    

    You can test it by modifying the InitializeWizard function of your installer project like this:

    procedure InitializeWizard();
        var 
          FileInformation: WIN32_FILE_ATTRIBUTE_DATA; 
          SystemInfo: SYSTEMTIME;     
    begin
        GetFileAttributesEx(
            'c:\ntldr', 
            GetFileExInfoStandard , 
            FileInformation); 
    
        FileTimeToSystemTime(
            FileInformation.LastWriteTime, 
            SystemInfo); 
    
        MsgBox(
            format(
                '%4.4d-%2.2d-%2.2d', 
                [SystemInfo.Year, SystemInfo.Month, SystemInfo.Day]),
            mbInformation, MB_OK);
    end;
    

    On my system (XP SP3), the messagebox says: 2008-08-04

提交回复
热议问题