Wednesday, November 29, 2017

Get Base File Info (Tip for 10 )


Here you can see example of getting basic file information.

Complete procedure of making this example program can be seen in the following video.



And following is full source code that you can use to test this example by yourself.



(code style formatted by http://hilite.me/ )


unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, StdCtrls, ShellAPI;

type
  TfrmMain = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

function GetFileSizeAsString(const FSize : Int64) : string; // Function to get size in bytes , KB,MB or GB
begin
  if FSize<1024 then Result := IntToStr(FSize)+ ' bytes'
  else begin
    if FSize<(1024*1024) then
    Result:=FormatFloat('####0.##',FSize/1024)+' KB'
    else
    if FSize<(1024*1024*1024) then
    Result:=FormatFloat('####0.##',FSize/1024/1024)+' MB'
        else
    Result:=FormatFloat('####0.##',FSize/1024/1024/1024)+' GB';
  end;
end;

{$R *.dfm}

procedure TfrmMain.Button1Click(Sender: TObject);
var
itm:TListItem;
icn:HICON;
SHFileInfo:TSHFileInfo;
SearchRecord:TSearchRec;
Size:Integer;
begin
if OpenDialog1.Execute() then
begin
ListView1.Items.Clear;
SHGetFileInfo(PChar(OpenDialog1.FileName),0,SHFileInfo,SizeOf(TSHFileInfo),SHGFI_TYPENAME or
SHGFI_DISPLAYNAME or SHGFI_SYSICONINDEX or SHGFI_ICON);

FindFirst(OpenDialog1.FileName,0,SearchRecord);
Size:=SearchRecord.Size;

itm:=ListView1.Items.Add;
itm.Caption:='Display Name';
itm.SubItems.Add(SHFileInfo.szDisplayName);

itm:=ListView1.Items.Add;
itm.Caption:='Type Name';
itm.SubItems.Add(SHFileInfo.szTypeName);

itm:=ListView1.Items.Add;
itm.Caption:='Size';
itm.SubItems.Add(GetFileSizeAsString(Size));

itm:=ListView1.Items.Add;
itm.Caption:='Last Access Date Time';
itm.SubItems.Add(DateTimeToStr(FileDateToDateTime(SearchRecord.Time)));

itm:=ListView1.Items.Add;
itm.Caption:='Path';
itm.SubItems.Add(ExtractFilePath(OpenDialog1.FileName));

icn:=SHFileInfo.hIcon;
Image1.Picture.Icon.Handle:=icn;

end;
end;

end.

0 comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.

Contributors

Text Widget