Delphi 获得文件大小的方法 大概有这些:
- FileSizeByName(需要引用IdGlobal单元 ,XE版本已取消)
- GetFileSize (use Windows ,仅支持windows)
- FileSize(不能获得正在使用的文件大小)
- FileSeek
- TFileStream.Size
1、FileSizeByName(需要引用IdGlobal单元,XE版本已取消) if OpenDialog1.Execute then begin ShowMessage(IntToStr(FileSizeByName(OpenDialog1.Filename))); end; 2、GetFileSize (use Windows ,仅支持windows) var FileHandle:integer; begin if OpenDialog1.Execute then begin FileHandle := FileOpen(OpenDialog1.FileName, 0); ShowMessage(IntToStr(GetFileSize(FileHandle, nil))); FileClose(FileHandle); end; end; 3、FileSize(不能获得正在使用的文件大小) var f: file; begin if OpenDialog1.Execute then begin AssignFile(f, OpenDialog1.FileName); Reset(f, 1); ShowMessage(IntToStr(FileSize(f))); CloseFile(f); end; end; 4、FileSeek var FileHandle:integer; begin if OpenDialog1.Execute then begin FileHandle := FileOpen(OpenDialog1.FileName, 0); ShowMessage(IntToStr(FileSeek(FileHandle,0,2))); FileClose(FileHandle); end; end; 5、TFileStream.Size var FS: TFileStream; begin if OpenDialog1.Execute then begin FS := TFileStream.Create(OpenDialog1.FileName, fmShareDenyNone); ShowMessage(IntToStr(FS.Size)); FS.Free; end; end;