我的目标是添加信息,参考每个文件(仅文件)的大小,必须保留在ListView
.
我在下面的尝试没有成功。哪里错了?
type
TForm1 = class(TForm)
btn1: TButton;
LV1: TListView;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ListSize: TStrings;
implementation
{$R *.dfm}
function ListFolders(Directory: String): string;
var
FileName, Dirlist: string;
SearchRec: TWin32FindData;
FindHandle: THandle;
ReturnStr: string;
begin
ReturnStr := '';
try
FindHandle := FindFirstFile(PChar(Directory + '*.*'), SearchRec);
if FindHandle <> INVALID_HANDLE_VALUE then
repeat
FileName := SearchRec.cFileName;
if ((SearchRec.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) then
Dirlist := Dirlist + (FileName + #13);
until FindNextFile(FindHandle, SearchRec) = False;
finally
Winapi.Windows.FindClose(FindHandle);
end;
ReturnStr := (Dirlist);
Result := ReturnStr;
end;
function FileSizeStr(FileName: string): string;
const
// K = Int64(1000); // Comment out this line OR
K = Int64(1024); // Comment out this line
M = K * K;
G = K * M;
T = K * G;
var
size: Int64;
handle: integer;
begin
handle := FileOpen(FileName, fmOpenRead);
if handle = -1 then
Result := 'Unable to open file ' + FileName
else
try
size := FileSeek(handle, Int64(0), 2);
if size < K then
Result := Format('%d bytes', [size])
else if size < M then
Result := Format('%f KB', [size / K])
else if size < G then
Result := Format('%f MB', [size / M])
else if size < T then
Result := Format('%f GB', [size / G])
else
Result := Format('%f TB', [size / T]);
finally
FileClose(handle);
end;
end;
function GetFiles(FileName, Ext: String): String;
Var
SearchFile: TSearchRec;
FindResult: integer;
ListFiles: TStrings;
begin
ListFiles := TStringlist.Create;
ListSize := TStringlist.Create;
FindResult := FindFirst(FileName + Ext, faArchive, SearchFile);
try
While FindResult = 0 do
begin
Application.ProcessMessages;
ListFiles.Add(SearchFile.Name);
ListSize.Add(FileSizeStr(FileName + SearchFile.Name));
FindResult := FindNext(SearchFile);
end;
finally
FindClose(SearchFile)
end;
Result := ListFiles.Text;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
L: TListItem;
vListFolders, vListSize, vListFiles: TStringlist;
i, j, k: integer;
begin
LV1.Clear;
vListFolders := TStringlist.Create;
vListFiles := TStringlist.Create;
vListSize := TStringlist.Create;
vListFolders.Text := ListFolders('C:\');
vListFiles.Text := GetFiles('C:\', '*.*');
vListSize.Text := ListSize.Text;
for i := 0 to vListFolders.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListFolders.Strings[i];
L.SubItems.Add('Folder');
end;
for j := 0 to vListFiles.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListFiles.Strings[j];
L.SubItems.Add('File');
end;
///////////////////////////////////////
for k := 0 to vListSize.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListSize.Strings[k];
// L.SubItems.Add(vListSize.Strings[k]);
end;
///////////////////////////////////////
vListFolders.Free;
vListFiles.Free;
vListSize.Free;
end;
版本:
我需要单独处理每一列,因为这些数据将来自我的远程管理应用程序的客户端,并且分别请求每个数据。首先是请求文件夹-> 来文件夹,在文件之后-> 来文件,最后是存储在客户端的每个文件的大小,与上面显示的方式相同。考虑
Button
像我的接收器(服务器端)的代码:D。
例如(接收文件夹,之后请求文件):
if Pos('<|Folder|>', s) > 0 then
begin
s2 := s;
Delete(s2, 1, Pos('<|Folder|>', s2) + 9);
s2 := Copy(s2, 1, Pos('<<|', s2) - 1);
Lista := TStringList.Create;
Lista.Text := s2;
// showmessage('ra');
L2 := Form1.LV1.FindCaption(0, intToStr(Socket.Handle), false, true, false);
(L2.SubItems.Objects[4] as TForm3).ListView1.Clear;
// Application.MessageBox(PChar(Lista.Text), ' ', 48);
for i := 0 to Lista.count - 1 do
begin
L := (L2.SubItems.Objects[4] as TForm3).ListView1.Items.Add;
L.ImageIndex := 0;
Sleep(10);
L.Caption := Lista.Strings[i];
L.SubItems.Add('Folder');
end;
if (L2.SubItems.Objects[4] as TForm3).ListView1.Items[0].Caption = '..' then
(L2.SubItems.Objects[4] as TForm3).ListView1.Items[0].ImageIndex := 5;
Lista.Free;
Socket.SendText('<|Files|>' + (L2.SubItems.Objects[4] as TForm3).Edit1.Text + '<<|');
end;