关于文件大小的问题(50分)

  • 主题发起人 主题发起人 nick4309
  • 开始时间 开始时间
N

nick4309

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,怎样用opendialog.filename得到一个文件的大小。我用filesize时又必须打开这个文件,而<br>且返回的是记录数,不是文件大小。而用getfilesize时又有文件句柄参数。用文件流又要打开文件,难到我了。请高首赐教,谢谢!
 
康夫 (2000-8-17 14:06:00) &nbsp;<br>1.<br>function FSize(FileName: string): LongInt;<br>var<br>&nbsp; F: File;<br>begin<br>&nbsp; AssignFile(F, FileName);<br>&nbsp; reset(F,1);<br>&nbsp; Result := FileSize(F);<br>&nbsp; CloseFile(F);<br>end;<br><br>&nbsp;<br>
 
to :hbezwwl<br>我说过不能用filesize,因为他返回的是记录文件的记录数,而不是文件大小,而且我不想打开文件!谢谢
 
function GetFileSize(const FileName: string): LongInt;<br>var<br>&nbsp; SearchRec: TSearchRec;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then<br>&nbsp; &nbsp; &nbsp; Result := SearchRec.Size<br>&nbsp; &nbsp; else Result := -1;<br>&nbsp; finally<br>&nbsp; &nbsp; SysUtils.FindClose(SearchRec);<br>&nbsp; end;<br>end;
 
没看见sorry
 
其实hbezwwl的方法是对的,返回的并不是记录数!! 因为reset(F, 1),<br>这就决定了是返回的字节。<br><br>另外,所有的办法都需要调用GetFileSize API,这就决定了都是要打开文件的,<br>只不过区别在与是你自己的代码打开还是别的代码打开而已。
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; function GetFileSize(const FileName: string): LongInt;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>function TForm1.GetFileSize(const FileName: string): LongInt;<br>var<br>&nbsp; SearchRec: TSearchRec;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then<br>&nbsp; &nbsp; &nbsp; Result := SearchRec.Size<br>&nbsp; &nbsp; else Result := -1;<br>&nbsp; finally<br>&nbsp; &nbsp; SysUtils.FindClose(SearchRec);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; i:integer;<br>begin<br>&nbsp; i:=self.GetFileSize('c:/windows/desktop/ss.bmp');<br>&nbsp; showmessage(inttostr(i));<br>end;<br><br>end.<br>同意zw84611.
 
后退
顶部