如何取得listbox中所有文件的总大小(20分)

  • 主题发起人 主题发起人 jog81
  • 开始时间 开始时间
J

jog81

Unregistered / Unconfirmed
GUEST, unregistred user!
我想实现取得listbox中所有文件的总大小,
如果它的个数指定比较好做,但是如果它的个数不确定,
由用户选定后才确定。
设m:=listbox.items.count; n:=listbox.itemindex;
当n从0到m-1时,如何取得总大小呢?
 
用for循环呀!
我看不懂题目,什么叫“由用户选定后才确定”?我理解为只计算选中的项目,那么
在for循环中加入一个if listbox1.selected then
begin
文件大小总和增加当前选中的文件大小
end
 
另:获取文件大小可以用FileSizeByName(文件全路径),要uses IDGlobal
 
procedure TForm1.Button1Click(Sender: TObject);
var
I : Integer;
FileHandle : THandle;
FileSize : Integer;
begin
FileSize := 0;
for I := 0 to FileListBox1.Items.Count-1 do
begin
if FileListBox1.Selected then
begin
FileHandle := FileOpen(DirectoryListBox1.Directory+'/'+FileListBox1.Items,fmOpenRead);
if FileHandle<> -1 then
begin
FileSize := FileSize+FileSeek(FileHandle,0,2);
end;
FileClose(FileHandle);
end;
end;
Button1.Caption := IntToStr(FileSize);
end;
 
多人接受答案了。
 
后退
顶部