【问题标题】:the correct way to use get size function in object pascal [duplicate]在对象pascal中使用get size函数的正确方法[重复]
【发布时间】:2015-08-15 07:50:08
【问题描述】:

我是 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;

【问题讨论】:

    标签: function delphi delphi-xe2


    【解决方案1】:

    试试这样的:

    ..., 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;
    

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 1970-01-01
      • 2023-03-06
      • 2020-04-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2021-06-24
      • 2015-09-09
      相关资源
      最近更新 更多