Mega Code Archive

 
Categories / Delphi / Files
 

SHGetFileInfo (API)

Title: SHGetFileInfo (API) Question: How to use the SHGetFileInfo API Answer: unit Unit1; // This app shows how to get different information about // a file, drive or directory. // !!! BE SURE TO INCLUDE 'ShellApi' IN YOUR USES-CLAUSE !!! interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ShellApi, FileCtrl; type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; Edit1: TEdit; Label2: TLabel; Edit2: TEdit; Label3: TLabel; Edit3: TEdit; Label4: TLabel; FileListBox1: TFileListBox; DirectoryListBox1: TDirectoryListBox; DriveComboBox1: TDriveComboBox; Label5: TLabel; Edit4: TEdit; Edit5: TEdit; Label6: TLabel; procedure Button1Click(Sender: TObject); procedure FormPaint(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; FileInfo : SHFILEINFO; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var filename : string; attrs : string; attributes : integer; begin form1.repaint; if filelistbox1.ItemIndex - 1 then begin filename := FileListBox1.FileName; end else begin filename := DirectoryListBox1.Directory; end; edit1.text := filename; //Get The DisplayName SHGetFileInfo(PChar(FileName),0,FileInfo,SizeOf(FileInfo),SHGFI_DISPLAYNAME); edit2.text := FileInfo.szDisplayName; //Get The TypeName SHGetFileInfo(PChar(FileName),0,FileInfo,SizeOf(FileInfo),SHGFI_TYPENAME); edit3.text := FileInfo.szTypeName; //Get The Icon That Represents The File SHGetFileInfo(PChar(FileName),0,FileInfo,SizeOf(FileInfo),SHGFI_ICON); Drawicon(canvas.handle,5,170,FileInfo.hIcon); form1.repaint; //Get The Files' Attributes attributes := GetFileAttributes(PChar(FileName)); attrs := ''; If Bool(attributes and FILE_ATTRIBUTE_READONLY) then attrs := attrs + 'R'; If Bool(attributes and FILE_ATTRIBUTE_HIDDEN) then attrs := attrs + 'H'; If Bool(attributes and FILE_ATTRIBUTE_SYSTEM) then attrs := attrs + 'S'; If Bool(attributes and FILE_ATTRIBUTE_ARCHIVE) then attrs := attrs + 'A'; edit4.text := attrs; //Get The Date & Time When The File Was Last Changed try edit5.text := DateTimeToSTr(FileDateToDateTime(FileAge(FileName))); except // In Case you didn't select a file (for directories & drives) on EConvertError do edit5.text := 'Not Available'; end; end; procedure TForm1.FormPaint(Sender: TObject); begin //Redraws the icon when you move the form off the screen. Drawicon(canvas.handle,5,170,FileInfo.hIcon); end; end.