GetFileSzie,GetFileTime執行結果有的正常, 有的異常 ? |
缺席
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation function GetFileSizeStr(sFilename : String):String; var hf: hFile; lwHFileSize, lwFilesize: longword; ofs : TOFStruct; i: integer; begin if FileExists(sFilename) then begin hf := OpenFile(pchar(sFilename), ofs, OF_READ or OF_WRITE or OF_SHARE_EXCLUSIVE); if hf <> 0 then begin // 取得檔案大小 lwFilesize := GetFileSize(hf, @lwHFileSize); //ShowMessage(IntToStr(lwFilesize) ' bytes'); Result := IntToStr(lwFilesize); _lclose(hf); end; end; end; function GetFileTimeStr(sFilename:String):String; var hf: hFile; ofs : TOFStruct; ftCreation: _FILETIME; ftLastAccess: _FILETIME; ftLastWrite: _FILETIME; sTime: _SYSTEMTIME; cTime: TDateTime; begin if FileExists(sFilename) then begin hf := OpenFile(pchar(sFilename), ofs, OF_READ or OF_WRITE or OF_SHARE_EXCLUSIVE); if hf <> 0 then begin GetFileTime(hf, @ftCreation, @ftLastAccess, @ftLastWrite); FileTimeToSystemTime(ftCreation, sTime); cTime := SystemTimeToDateTime(sTime); Result:=DateTimeToStr(cTime); _lclose(hf); end; end; end; {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var sFileName : String; begin Memo1.Clear; sFileName:='c:\windows\system32\drivers\ALCXWDM.SYS'; Memo1.Lines.Add(sFileName); Memo1.Lines.Add(GetFileTimeStr(sFileName)); Memo1.Lines.Add(GetFileSizeStr(sFileName)); Memo1.Lines.Add(''); sFileName:='c:\windows\System32\svchost.exe'; Memo1.Lines.Add(sFileName); Memo1.Lines.Add(GetFileTimeStr(sFileName)); Memo1.Lines.Add(GetFileSizeStr(sFileName)); end; end.執行結果, 為何有的正常, 有的異常 ? 謝謝 ! c:\windows\system32\drivers\ALCXWDM.SYS 2004/3/17 上午 04:28:02 701676 c:\windows\System32\svchost.exe 1662/7/5 下午 05:47:12 4294967295 Windows XP PRO 下目前測試有問題的 c:\windows\system32\svchost.exe C:\AppServ\Apache\Apache.exe c:\windows\System32\DRIVERS\atapi.sys c:\windows\system32\services.exe C:\Program Files\Common Files\Microsoft Shared\VS7DEBUG\MDM.EXE C:\AppServ\mysql\bin\mysqld-nt.exe c:\windows\System32\lsass.exe C:\oracle\ora92\bin\omtsreco.exe C:\oracle\ora92\bin\agntsrvc.exe c:\oracle\ora92\bin\ORACLE.EXE c:\windows\System32\SCardSvr.exe c:\windows\system32\spoolsv.exe C:\WINDOWS\System32\vmnetdhcp.exe C:\WINDOWS\System32\vmnat.exe 發表人 - pcboy2 於 2004/09/24 15:37:58
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! | |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
正確可用的 GetFileDate , GetFileSizeStr 還在測試中...
function GetFileDate(const FileName: string; out Creation, LastAccess, LastWrite: TDateTime): Boolean; var hFile: THandle; ftCreationUTC, ftLastAccessUTC, ftLastWriteUTC: TFileTime; ftCreationLocal, ftLastAccessLocal, ftLastWriteLocal: TFileTime; stCreationLocal, stLastAccessLocal, stLastWriteLocal: TSystemTime; begin result:=false; hFile := CreateFile(PChar(FileName), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); if (hFile <> INVALID_HANDLE_VALUE) then begin try if GetFileTime(hFile, @ftCreationUTC, @ftLastAccessUTC, @ftLastWriteUTC) then begin if FileTimeToLocalFileTime(ftCreationUTC, ftCreationLocal) and FileTimeToLocalFileTime(ftLastAccessUTC, ftLastAccessLocal) and FileTimeToLocalFileTime(ftLastWriteUTC, ftLastWriteLocal) then begin if FileTimeToSystemTime(ftCreationLocal, stCreationLocal) and FileTimeToSystemTime(ftLastAccessLocal, stLastAccessLocal) and FileTimeToSystemTime(ftLastWriteLocal, stLastWriteLocal) then begin Creation := SystemTimeToDateTime(stCreationLocal); LastAccess := SystemTimeToDateTime(stLastAccessLocal); LastWrite := SystemTimeToDateTime(stLastWriteLocal); result:=true; end; end; end; finally CloseHandle(hFile); end; end; end; procedure TForm1.Button1Click(Sender: TObject); var creation, lastaccess, lastwrite: TDateTime; begin GetFileDate('c:\winnt\System32\svchost.exe', creation, lastaccess, lastwrite); ShowMessage('caeation: ' DateTimeToStr(creation) #13#10 'last access: ' DateTimeToStr(lastaccess) #13#10 'last write: ' DateTimeToStr(lastwrite)); end;
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
正確取得檔案大小 GetFileSize
function GetFileSize(const FileName:string):longint; var SearchRec:TSearchRec; begin if FindFirst(ExpandFileName(FileName),faAnyFile,SearchRec)=0 then Result:=SearchRec.Size else Result:=-1; FindClose(SearchRec); end;
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
|
ostyle
一般會員 發表:0 回覆:1 積分:0 註冊:2007-02-07 發送簡訊給我 |
我想也許是使用取得檔案資料的方式不同,使它取得的時間有所差異… 我試了一下…發現使用findfirstfile…沒有什麼誤差…也許可以參考看看… 只不過…哈…這篇文章過了很久了就是了~
------
1234 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |