1. 첫번째 방법
3. 기타 다양한 방법들(취사 선택해서 쓰세요)
// returns file size in bytes or -1 if not found.
function FileSize(fileName : wideString) : Int64;
var
sr : TSearchRec;
begin
if FindFirst(fileName, faAnyFile, sr ) = 0 then
result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
else
result := -1;
FindClose(sr) ;
end;
2. 두번째 방법 function GetFileSize(AFileName: string; ASkip: boolean = false):
Integer;
var
f: file of Byte;
size: Longint;
flag: boolean;
begin
flag := false;
while not flag do
begin
Application.ProcessMessages;
if FStopFlag then
break;
if FileExists(AFileName) then
begin
try
size := 0;
AssignFile(f, AFileName);
reset(f);
size := FileSize(f);
CloseFile(f);
flag := true;
except
on e:Exception do
begin
flag := ASkip;
size := -1;
end;
end;
end;
end;
result := size;
end;
flag 는 한번에 파일 사이즈를 못얻는 경우(파일이 오픈되었다가 close가 되지 않았을 경우 등)를 대비해 여러번 시행하기 위함임, while 문을 빼버려도 됨 3. 기타 다양한 방법들(취사 선택해서 쓰세요)
function Get_File_Size1(sFileToExamine: string; bInKBytes: Boolean): string;
{
for some reason both methods of finding file size return
a filesize that is slightly larger than what Windows File
Explorer reports
}
var
FileHandle: THandle;
FileSize: LongWord;
d1: Double;
i1: Int64;
begin
//a- Get file size
FileHandle := CreateFile(PChar(sFileToExamine),
GENERIC_READ,
0, {exclusive}
nil, {security}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
FileSize := GetFileSize(FileHandle, nil);
Result := IntToStr(FileSize);
CloseHandle(FileHandle);
//a- optionally report back in Kbytes
if bInKbytes = True then
begin
if Length(Result) > 3 then
begin
Insert('.', Result, Length(Result) - 2);
d1 := StrToFloat(Result);
Result := IntToStr(round(d1)) + 'KB';
end
else
Result := '1KB';
end;
end;
{******************************************************************************
Thanks to Advanced Delphi Systems here's another method which works just as
well returning the same results
*******************************************************************************}
function Get_File_Size2(sFileToExamine: string; bInKBytes: Boolean): string;
var
SearchRec: TSearchRec;
sgPath: string;
inRetval, I1: Integer;
begin
sgPath := ExpandFileName(sFileToExamine);
try
inRetval := FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec);
if inRetval = 0 then
I1 := SearchRec.Size
else
I1 := -1;
finally
SysUtils.FindClose(SearchRec);
end;
Result := IntToStr(I1);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
label1.Caption := Get_File_Size(Opendialog1.FileName, True);
end;
{*******************************************************************************}
function Get_File_Size3(const FileName: string): TULargeInteger;
// by nico
var
Find: THandle;
Data: TWin32FindData;
begin
Result.QuadPart := -1;
Find := FindFirstFile(PChar(FileName), Data);
if (Find <> INVALID_HANDLE_VALUE) then
begin
Result.LowPart := Data.nFileSizeLow;
Result.HighPart := Data.nFileSizeHigh;
Windows.FindClose(Find);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if (OpenDialog1.Execute) then
ShowMessage(IntToStr(Get_File_Size3(OpenDialog1.FileName).QuadPart));
end;
{*******************************************************************************}
function Get_File_Size4(const S: string): Int64;
var
FD: TWin32FindData;
FH: THandle;
begin
FH := FindFirstFile(PChar(S), FD);
if FH = INVALID_HANDLE_VALUE then Result := 0
else
try
Result := FD.nFileSizeHigh;
Result := Result shl 32;
Result := Result + FD.nFileSizeLow;
finally
CloseHandle(FH);
end;
end;
'프로그래밍 > Delphi' 카테고리의 다른 글
| Twitter for Delphi(델파이를 이용한 트위터 클라이언트 제작) (3) | 2011.07.13 |
|---|---|
| TPrinter를 이용한 이미지 인쇄 (3) | 2011.03.16 |
| SMTP를 이용하여 메일 발송시 아이폰에서 한글 깨지는 문제 처리 (0) | 2011.02.10 |
| 설정된 프린터 용지 얻기 및 설정하기 (1) | 2011.01.30 |
| virtual 과 dynamic 그리고 override 의 내부 메카니즘 소개 (2) | 2010.08.04 |