首页  编辑  

文件或目录转换成TreeView

Tags: /超级猛料/Stream.File.流、文件和目录/目录/   Date Created:

下面的这个函数就可以了:

procedure DirToTreeView ( Tree : TTreeView ; Directory : string ; Root : TTreeNode ; IncludeFiles :

 Boolean );

var

 SearchRec         : TSearchRec ;

 ItemTemp           : TTreeNode ;

begin

  with Tree . Items do

  try

   BeginUpdate ;

    if Directory [ Length ( Directory )] <> '\' then Directory := Directory + '\' ;

    if FindFirst ( Directory + '*.*' , faDirectory , SearchRec ) = 0 then

    begin

      repeat

        if ( SearchRec . Attr and faDirectory = faDirectory ) and ( SearchRec . Name [ 1 ] <> '.' ) then

        begin

          if ( SearchRec . Attr and faDirectory > 0 ) then

           Root := AddChild ( Root , SearchRec . Name );

         ItemTemp := Root . Parent ;

         DirToTreeView ( Tree , Directory + SearchRec . Name , Root , IncludeFiles );

         Root := ItemTemp ;

        end

        else if IncludeFiles then

          if SearchRec . Name [ 1 ] <> '.' then

           AddChild ( Root , SearchRec . Name );

      until FindNext ( SearchRec ) <> 0 ;

     FindClose ( SearchRec );

    end ;

  finally

   EndUpdate ;

  end ;

end ;