I'm making simple file manager, using Delphi. I use ListView
to show folders and files and I use FileList
(it's not visible) to get files and folders names from some directory.
The problem is that I want to not include this [.]
element (it's symbol of current dir, don't know why Delphi's FileList
shows it) in ListView. But, when I try to ignore it(here's code)
procedure TfolderFrame.ShowFiles;
var
i: Integer;
size: int64;
fileName, extension: string;
begin
edt1.Text := CurrentFullPath;
lvListView.Clear;
fllstFiles.Directory := CurrentFullPath;
For i := 0 To fllstFiles.Items.Count-1 Do begin
fileName := fllstFiles.Items.Strings[i];
extension := UpperCase(ExtractFileExt(fileName));
size := DSiFileSize(fileName);
Delete(extension, 1, 1);
if (fileName <> '[.]') then begin //error apperas at this line!
if (not(isDirectory(fileName))) then begin
lvListView.Items.Add.Caption := fileName;
lvListView.Items[i].SubItems.Add(IntToStr(size));
lvListView.Items[i].SubItems.Add(extension);
lvListView.Items[i].ImageIndex := GetItemImage(fileName, extension);
end
else begin
Delete(fileName, 1, 1);
Delete(fileName, Length(fileName), 1);
lvListView.Items.Add.Caption := fileName;
lvListView.Items[i].ImageIndex := GetItemImage(fileName, extension);
end;
end;
end;
end;
an error appers
Access violation at address 004634DF in module file_manager.exe. Read of adress 00000010
What I've done wrong?
You're not adding an item in each iteration in the for loop. When you skip for [.]
, you should get an AV for lvlistview.items[i]
in the next iteration, because the index of the item you just added is i-1
. To resolve, keep a reference to the item you add and work on it.
var
..
Item: TListItem;
begin
..
Item := lvListView.Items.Add;
Item.Caption := fileName;
Item.SubItems.Add(IntToStr(size));
Item.SubItems.Add(extention);
Item.ImageIndex := GetItemImage(fileName, extention);
..