جواب به سوال

دوست عزيزی ايميل زده بودند و اين سوال را پرسيده بودند:

Salam aziz
mamnoon az site ghashanget.
agha man khodam ro koshtam ta in code name file ro size oon file bokoneh
ama ba khtaye "FILE ACCESS DINIDEd" movajeh mishavam.
agha kheyli mamnoon misham az javabet.

function GetDirSize(dir: string; subdir: Boolean): Longint;
var
rec: TSearchRec;
found: Integer;
f:File;
st:String;
begin
Result := 0;
if dir[Length(dir)] <> '\' then dir := dir + '\';
found := FindFirst(dir + '*.*', faAnyFile, rec);
while found = 0 do
begin
Inc(Result, rec.Size);
AssignFile(f, rec.Name);
st:=inttostr(rec.Size);
Rename(f,st);
if (rec.Attr and faDirectory > 0) and (rec.Name[1] <> '.') and (subdir = True) then
Inc(Result, GetDirSize(dir + rec.Name, True));
found := FindNext(rec);
end;
FindClose(rec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := FloatToStr(GetDirSize('C:\WIN98\Desktop\percent', False) / Sqr(1024)) + ' MBytes';
label2.Caption := FloatToStr(GetDirSize('C:\WIN98\Desktop\percent', True) / Sqr(1024)) + ' MBytes';
end;

در جواب اين سوال بايد بگم که شما می توانيد از توابع API براي اين كار استفاده كنيد كه اسم آنها اين است: _lopen , _lclose , GetFileSize

unit GetFileSizeU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
OpenDialog1: TOpenDialog;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.text:=opendialog1.filename;
end;

procedure TForm1.Button2Click(Sender: TObject);
var h1:integer;
n1:integer;
begin
h1:=_lopen(pchar(edit1.text),OF_READ);
n1:=GetFileSize(h1,nil);
edit2.text:=inttostr(n1);
_lclose(h1);
end;

end.   
نویسنده : محمد ; ساعت ۱۱:٢۸ ‎ق.ظ روز پنجشنبه ٤ اردیبهشت ،۱۳۸٢