請問如何取得檔案的所有一般資訊 ? |
答題得分者是:hagar
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
老大仔
尊榮會員 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
是以下這些嗎??
[code delphi] //取得建立日期 Memo1.Lines.Add(DateTimeToStr(GetFileCreationTime('C:\aaa.txt'))); //取得修改時間 Memo1.Lines.Add(DateTimeToStr(FileDateToDateTime(FileAge('C:\aaa.txt')))); //取得檔案完整路徑 Memo1.Lines.Add(ExpandFileName('C:\aaa.txt')); //取得副檔名 Memo1.Lines.Add(ExtractFileExt('C:\aaa.txt')); [/code] ===================引 用 pcboy 文 章=================== 請問如何取得檔案的所有一般資訊 ? 謝謝 |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
http://www.scip.be/index.php?Page=ArticlesDelphi06&Lang=EN 這個程式需要 uses shellapi; (原網頁上沒提) 小弟測試可以用,全部程式如下 [code delphi] // Delphi 7 // 取得檔案按滑鼠右鍵後,選 [一般] 看到的資訊 // 請放一個 Button1 和一個 Memo1 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type TFileInfo = record Icon : hIcon; Image : Integer; DisplayName : String; TypeName : String; Size : Integer; SizeDescription : String; DateTime : TDateTime; AttrArchive : Boolean; AttrReadOnly : Boolean; AttrSystem : Boolean; AttrHidden : Boolean; AttrVolume : Boolean; AttrDirectory : Boolean; end; var Form1: TForm1; implementation {$R *.dfm} uses shellapi; // ---------------------------------------------------------------- // Return string with formatted file size (bytes, Kb, Mb or Gb) // ---------------------------------------------------------------- function scGetSizeDescription(const IntSize : Int64) : String; begin if IntSize < 1024 then Result := IntToStr(IntSize) ' bytes' else begin if IntSize < (1024 * 1024) then Result := FormatFloat('####0.##',IntSize / 1024) ' Kb' else if IntSize < (1024 * 1024 * 1024) then Result := FormatFloat('####0.##',IntSize / 1024 / 1024) ' Mb' else Result := FormatFloat('####0.##',IntSize / 1024 / 1024 / 1024) ' Gb'; end; end; // ---------------------------------------------------------------- // Return record with all information about given file // How to use icon : ImageFile.Picture.Icon.Handle:=Info.Icon; // ---------------------------------------------------------------- procedure scGetFileInfo(StrPath : String; var Info : TFileInfo); var SHFileInfo : TSHFileInfo; SearchRec : TSearchRec; begin if Trim(StrPath) = '' then Exit; ShGetFileInfo(PChar(StrPath), 0, SHFileInfo, SizeOf (TSHFileInfo), SHGFI_TYPENAME or SHGFI_DISPLAYNAME or SHGFI_SYSICONINDEX or SHGFI_ICON); with Info do begin Icon := SHFileInfo.hIcon; Image := SHFileInfo.iIcon; DisplayName := SHFileInfo.szDisplayName; TypeName := SHFileInfo.szTypeName; end; FindFirst(StrPath, 0, SearchRec); with Info do begin try DateTime := FileDateToDateTime(SearchRec.Time); except DateTime := Now(); end; AttrReadOnly := ((SearchRec.Attr and faReadOnly) > 0); AttrSystem := ((SearchRec.Attr and faSysFile) > 0); AttrHidden := ((SearchRec.Attr and faHidden) > 0); AttrArchive := ((SearchRec.Attr and faArchive) > 0); AttrVolume := ((SearchRec.Attr and faVolumeID) > 0); AttrDirectory := ((SearchRec.Attr and faDirectory) > 0); Size := SearchRec.Size; SizeDescription := scGetSizeDescription(Size); end; end; procedure TForm1.Button1Click(Sender: TObject); var Info : TFileInfo; StrPath : String; begin StrPath := 'C:\WINDOWS\notepad.exe'; scGetFileInfo(StrPath, Info); with Info do begin Memo1.Lines.Clear; // Icon : hIcon; Memo1.Lines.Add('Image : ' IntToStr(Image)); Memo1.Lines.Add('DisplayName : ' DisplayName); Memo1.Lines.Add('TypeName : ' TypeName); Memo1.Lines.Add('Size : ' IntToStr(Size)); Memo1.Lines.Add('SizeDescription : ' SizeDescription); Memo1.Lines.Add('DateTime : ' DateTimeToStr(DateTime)); Memo1.Lines.Add('AttrArchive : ' BoolToStr(AttrArchive, true)); Memo1.Lines.Add('AttrReadOnly : ' BoolToStr(AttrReadOnly, true)); Memo1.Lines.Add('AttrSystem : ' BoolToStr(AttrSystem, true)); Memo1.Lines.Add('AttrHidden : ' BoolToStr(AttrHidden, true)); Memo1.Lines.Add('AttrVolume : ' BoolToStr(AttrVolume, true)); Memo1.Lines.Add('AttrDirectory : ' BoolToStr(AttrDirectory, true)); end; end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
"磁碟大小" (Size on disk) 的範例可以參考下面 [code delphi] // Delphi 7 // Get FileSize, Size on disk, cluster size unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function GetClusterSize(Drive: Char): Longint; var SectorsPerCluster, BytesPerSector, dummy: Cardinal; begin SectorsPerCluster := 0; BytesPerSector := 0; GetDiskFreeSpace( PChar(Drive ':\'), SectorsPerCluster, BytesPerSector, dummy, dummy ); result := SectorsPerCluster * BytesPerSector; end; function FileSizeEx(const Filename: string; var AFileSize, ASizeOnDisk, AClusterSize: int64): boolean; var SR : TSearchRec; n : integer; begin if FindFirst(Filename, faAnyFile, SR) = 0 then try AFileSize := SR.FindData.nFileSizeHigh; AFileSize := (AFileSize shl (4 *sizeof(AFileSize))) or SR.FindData.nFileSizeLow; AClusterSize := GetClusterSize( UpCase(Filename[1]) ); n := AFileSize mod AClusterSize; if n > 0 then ASizeOnDisk := AFileSize (AClusterSize -n) else ASizeOnDisk := AFileSize; result := true; finally FindClose(SR); end; end; //Example... procedure TForm1.Button1Click(Sender: TObject); var AFileSize, ASizeOnDisk, AClusterSize : int64; begin if FileSizeEx(ParamStr(0), AFileSize, ASizeOnDisk, AClusterSize) then ShowMessage( ParamStr(0) #10 Format('File size: %.n bytes', [AFileSize *1.0]) #10 Format('Size on disk: %.n bytes', [ASizeOnDisk *1.0]) #10 Format('Cluster size: %.n bytes', [AClusterSize *1.0]) ) else ShowMessage('FileSizeEx() FAILED'); end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
取得檔案 建立、修改、存取 日期時間
[code delphi] // Delphi 7 // ================================================================ // Return the three dates (Created,Modified,Accessed) // of a given filename. Returns FALSE if file cannot // be found or permissions denied. Results are returned // in TdateTime VAR parameters // ================================================================ unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses ShellAPI; function GetFileTimes(FileName : string; var Created : TDateTime; var Modified : TDateTime; var Accessed : TDateTime) : boolean; var FileHandle : integer; Retvar : boolean; FTimeC,FTimeA,FTimeM : TFileTime; LTime : TFileTime; STime : TSystemTime; begin FileHandle := FileOpen(FileName,fmShareDenyNone); Created := 0.0; Modified := 0.0; Accessed := 0.0; if FileHandle < 0 then RetVar := false else begin RetVar := true; GetFileTime(FileHandle,@FTimeC,@FTimeA,@FTimeM); FileClose(FileHandle); // Created FileTimeToLocalFileTime(FTimeC,LTime); if FileTimeToSystemTime(LTime,STime) then begin Created := EncodeDate(STime.wYear,STime.wMonth,STime.wDay); Created := Created EncodeTime(STime.wHour,STime.wMinute,STime.wSecond,STime.wMilliSeconds); end; // Accessed FileTimeToLocalFileTime(FTimeA,LTime); if FileTimeToSystemTime(LTime,STime) then begin Accessed := EncodeDate(STime.wYear,STime.wMonth,STime.wDay); Accessed := Accessed EncodeTime(STime.wHour,STime.wMinute,STime.wSecond,STime.wMilliSeconds); end; // Modified FileTimeToLocalFileTime(FTimeM,LTime); if FileTimeToSystemTime(LTime,STime) then begin Modified := EncodeDate(STime.wYear,STime.wMonth,STime.wDay); Modified := Modified EncodeTime(STime.wHour,STime.wMinute,STime.wSecond,STime.wMilliSeconds); end; end; Result := RetVar; end; procedure TForm1.Button1Click(Sender: TObject); var CDate,MDate,ADate : TDateTime; begin if GetFileTimes('c:\windows\notepad.exe',CDate,MDate,ADate) then begin Label1.Caption := 'File Created DateTime : ' FormatDateTime('dd/mm/yyyy hh:nn',CDate); Label2.Caption := 'File Modified DateTime : ' FormatDateTime('dd/mm/yyyy hh:nn',MDate); Label3.Caption := 'File Accessed DateTime : ' FormatDateTime('dd/mm/yyyy hh:nn',ADate); end; end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |