Главная » Delphi » Интернет и сети » Как получить время изменения файла. [wininet]

Как получить время изменения файла. [wininet]

19:39
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, wininet, StdCtrls, DateUtils;
 
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
function GetUrlInfo(const dwInfoLevel: DWORD; const FileURL: string):
  string;
var
  hSession, hFile: hInternet;
  dwBuffer: Pointer;
  dwBufferLen, dwIndex: DWORD;
begin
  Result := '';
  hSession := InternetOpen('',
    INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(hSession) then begin
    hFile := InternetOpenURL(hSession, PChar(FileURL), nil, 0,
      INTERNET_FLAG_RELOAD, 0);
    dwIndex := 0;
    dwBufferLen := 20;
    if HttpQueryInfo(hFile, dwInfoLevel, @dwBuffer, dwBufferLen, dwIndex)
      then Result := PChar(@dwBuffer);
    if Assigned(hFile) then InternetCloseHandle(hFile);
    InternetCloseHandle(hsession);
  end;
end;
 
function GetFileTime(url: string): TDateTime;
var
  hSession, hFile: hInternet;
  dwBuffer: Pointer;
  dwBufferLen, dwIndex: DWORD;
  Len: DWORD;
  t, LastModified: SYSTEMTIME;
  ft1, ft2: FILETIME;
 
begin
 
  hSession := InternetOpen('',
    INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(hSession) then begin
    hFile := InternetOpenURL(hSession, PChar(URL), nil, 0,
      INTERNET_FLAG_RELOAD, 0);
    dwIndex := 0;
    Len := sizeof(LastModified);
    HttpQueryInfo(HFile, HTTP_QUERY_LAST_MODIFIED or
      HTTP_QUERY_FLAG_SYSTEMTIME, @LastModified, Len, dwIndex);
    if Len > 0
      then begin
      SystemTimeToFileTime(LastModified, ft2);
      FileTimeToLocalFileTime(ft2, ft1);
      FileTimeToSystemTime(ft1, t);
      result := EncodeDateTime(t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond, 0);
    end;
    if Assigned(hFile) then InternetCloseHandle(hFile);
    InternetCloseHandle(hsession);
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  s, url: string;
begin
  url := 'http://www.ya.ru/logo.gif';
  s := formatDatetime('dd mmmm yyyy, hh:nn:ss', GetFileTime(url));
  label1.Caption := s;
end;
 
end.