2015-08-15 35 views
-1

即時通訊在DELPHI初學者和我有一個問題.. 如何調用此函數並使用它,我想在一個標籤中顯示結果,也如何聲明功能正確,非常感謝。正確的方式使用獲取大小功能在對象帕斯卡爾

function FindFileSize(Filename:string):integer; 
var 
sr : TSearchRec; 
begin 
if FindFirst(filename,faAnyFile-faDirectory,sr) = 0 then 
Result := sr.Size 
else 
raise EFileNotFoundException.Create(filename+' not found.'); 
FindClose(sr); 
end; 

回答

0

嘗試這樣:

..., SysUtils; 

type 
    EFileNotFoundException = class(Exception) 
    end; 

function FindFileSize(const Filename: string): Int64; 
var 
    sr : TSearchRec; 
    Err: Integer; 
begin 
    Err := FindFirst(filename, faAnyFile and (not faDirectory), sr); 
    if Err = 0 then 
    begin 
    FindClose(sr); 
    if (sr.Attr and faDirectory) = 0 then 
    begin 
     Result := sr.Size; 
     Exit; 
    end; 
    Err := ERROR_FILE_NOT_FOUND; 
    end 
    if Err = ERROR_FILE_NOT_FOUND then begin 
    raise EFileNotFoundException.Create(filename + ' not found.'); 
    end else begin 
    RaiseLastOSError(Err); 
    end; 
end; 

procedure TMyForm.Button1Click(Sender: TObject); 
begin 
    Label1.Caption := IntToStr(FindFileSize('C:\path to\some file.ext')); 
end;