请教获取文件大小的最简单的方法(50分)

  • 主题发起人 主题发起人 陈冲伟
  • 开始时间 开始时间

陈冲伟

Unregistered / Unconfirmed
GUEST, unregistred user!
请教获取一个文件大小的最简单的方法
 
function FileLength(Filename:string):integer;<br>var<br>&nbsp; fp:file of byte;<br>begin<br>&nbsp; AssignFile(fp,Filename);<br>&nbsp; Reset(fp);<br>&nbsp; Result := FileSize(fp);<br>&nbsp; CloseFile(fp);<br>end;<br>或者<br>var<br>&nbsp; hFile : Integer;<br>begin<br>&nbsp; hFile:=CreateFile(PChar('c:/temp/a.htm'), GENERIC_READ, 0, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);<br>&nbsp; Edit1.Text := IntToStr(GetFileSize(hFile, 0));<br>end;
 
unreal提到的<br>function FileLength(Filename:string):integer;<br>var<br>&nbsp; fp:file of byte;<br>begin<br>&nbsp; AssignFile(fp,Filename);<br>&nbsp; Reset(fp);<br>&nbsp; Result := FileSize(fp);<br>&nbsp; CloseFile(fp);<br>end;<br>比后者好
 
win32api:<br>_lopen<br>GetFileSize<br>_lclose
 
如果该文件被别的程序独占打开unreal的办法就不行了。<br>findfirst可以解决这个问题。FindFirst(Path,faanyfile,F);f.size
 
对文本文件行不行,好象是不行
 
上面的方法对自己就不行了,会有I/O错误<br>要:<br>&nbsp; &nbsp; &nbsp; &nbsp; if TmpOicqMsg &lt;&gt; LastOicqMessage then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LastOicqMessage := TmpOicqMsg;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Messagebox(self.handle, PChar('OICQ用户' + TmpOicqNum + '对你的探测作出回应:' + #$0D + #$0A + TmpOicqMsg), '警告信息', MB_ICONWARNING + mb_ok);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br><br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; FileStream: TFileStream;<br>&nbsp; Ssize: string;<br>&nbsp; Isize: integer;<br>begin<br>&nbsp; FileStream := TFileStream.Create(Application.ExeName, fmShareDenyNone);<br>&nbsp; Isize := FileStream.Size;<br>&nbsp; FileStream.Free;<br>&nbsp; if Isize &lt;&gt; 139776 then //字节数与原字节数不符,可能文件被种了“木马”<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Ssize := inttostr(Isize);<br>&nbsp; &nbsp; &nbsp; Application.MessageBox(pchar('原文件字节数为139776字节,现在文件字节数' + #13 + '为' + Ssize + '字节,文件已被修改,请立刻查毒!'), pchar('警告信息!'), MB_ICONERROR + MB_OK);<br>&nbsp; &nbsp; &nbsp; Application.Terminate; //退出<br>&nbsp; &nbsp; end;<br>end;
 
请问terry_lzs兄:<br>&nbsp; &nbsp;为什么function FileLength(Filename:string):integer;<br>var<br>&nbsp; fp:file of byte;<br>begin<br>&nbsp; AssignFile(fp,Filename);<br>&nbsp; Reset(fp);<br>&nbsp; Result := FileSize(fp);<br>&nbsp; CloseFile(fp);<br>end;<br>&nbsp; &nbsp;比较好一些呢?<br>
 
陈冲伟:如果你还要继续讨论请定期提前你的帖子,如果不想继续讨论请结束帖子。<br>
 
用GetFileSize即可.
 
多人接受答案了。
 
后退
顶部