Point a TTreeView at C\

How do I tell a TTreeView to display C:\ ?
It has methods to manipulate the nodes once the tree is loaded, and methods to load it manually with individual items,  but I can't find a way to just point it at C:\ and have it fill itself with all the directories of C:\.
LoadFromFile('C:\') =  error.

Who is Participating?
LMuadDIbConnect With a Mentor Commented:
Ok, you would probably use the delphi functions "FindFirst", "FindNext" and "FindClose" to do this.
there are 2 ways to go about this I think. depending on what you need to be done.

1 way is to recursively search thru a directory or partition and throw ALL the results into the treeview.
This way can be slow and intensive especially if your trying to gather a large directory structure. But once its done, its done.

2nd way is to do it directory by directory whenever the user enters a certain directory.

A great example of a recursive search with a TTreeview can be found here:

I also have a recursive dir search function in a console application I made, which I can post if the above example doesnt help you.
But basically its the same as above example.

for the 2nd way you would have to use something similiar to this:

procedure ListFileDir(Path: string; tv: TTreeview);
  SR: TSearchRec;
  TempNode : TTreeNode;
  if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
      if (SR.Attr = faDirectory) then
        TempNode := tv.Items.Add(nil, SR.Name); { Add a root node }
        { give it an image and other stuff next }
        TempNode.ImageIndex := 0; { Dir Image Generic }
      if (SR.Attr <> faDirectory      ) then
        TempNode := tv.Items.Add(nil, SR.Name); { Add a root node }
        { give it an image and other stuff next }
        TempNode.ImageIndex := 1; { File Image Generic }
    until FindNext(SR) <> 0;

procedure TForm1.Button1Click(Sender: TObject);
  ListFileDir(edit1.Text, treeview1);

Of course this is an extremely simple example, and it only puts the dir listing in the root of treeview.
You would have to keep track of the slected treeview node, and insert the listing inside that node etc...
Its like I said, it will take quite a bit of code to do it manually

My advise, would be to use the recursive method.
hope this helps
Not possible to load a directory structure directly into a treeview.
you would have to parse evreything manually and insert them yourselve.

you should try some 3rd party components like LsFileExplorer & LsFileList... both are freeware w/ source and it will save you alot of time.
you can get it here: http://delphi.icm.edu.pl/ftp/d30free/FileExpl.zip 
frogAuthor Commented:
Thanks  LMuadDIb,
They are impressive components!

I want to use a TgfglTreeView which has a checkbox at each node for selection of directories.
(Both it and TTreeview say they can be used for display/selection of disk directories, they forget to say that it is very unwieldy to attempt to use them in that way!)

it is possible,
download a working example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21041851.zip        Finding node in a treeshellview
frogAuthor Commented:
Thanks  LMuadDIb,
The link provided a good example (and plenty of other good info!). I have adapted the example and it does exactly what I want.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.