Mirror

Display hierarchical drive information in a TTreeView (Views: 100)


Problem/Question/Abstract:

How can I insert a hierarchy drive in a TTreeView? I would liket to insert for example my drive C:\ in the treeview.

Answer:

procedure FilePathToTreeNode(aTreeView: TTreeView; aRoot: TTreeNode;
  Path: string; Recurse: boolean);
var
  NewNode: TTreeNode;
  SRec: TSearchRec;
begin
  if FindFirst(Path + '*.*', SysUtils.faAnyFile, SRec) = 0 then
    repeat
      if (sRec.Name = '.') or (sRec.Name = '..') then
        Continue;
      NewNode := aTreeView.Items.AddChild(aRoot, SRec.Name);
      if Recurse and ((srec.Attr and sysutils.faDirectory) <> 0) then
        FilePathToTreeNode(aTreeView, NewNode, Path + srec.name + '\', True);
    until
      FindNext(SRec) <> 0;
end;

Call it like this:

FilePathToTreeNode(TreeView1, nil, 'c:\', True);

Consider using ShellTreeView and ShellListView from the samples component page (at least in D6).

<< Back to main page