怎么去一文件夹下所有文件夹和子文件夹的名称?
在线等ing!

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
     mydir : TSearchRec;//记录文件情况
    begin
    Memo1.Clear;
    if FindFirst('d:\tools\*.*',faDirectory,mydir) = 0 then
      begin
           Memo1.Lines.Add(mydir.Name);//可以把目录名记录在mystrings中,以后再用
        while FindNext(mydir) = 0 do
        begin
           Memo1.Lines.Add(mydir.Name);
        end;
      end;
    findclose(mydir);
    end;
      

  2.   

    我写的一个把指定目录显示到界面上的TreeView控件中的例子,希望能对你有用。unit MainFormUnit;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, ShlObj;type
      TMainForm = class(TForm)
        TV: TTreeView;
        btnInit: TButton;
        edtPath: TEdit;
        Label1: TLabel;
        btnSelectDir: TButton;
        procedure btnInitClick(Sender: TObject);
        procedure btnSelectDirClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;procedure InitTree(TreeNodes:TTreeNodes;ParentNode:TTreeNode;Path:AnsiString);
    implementation{$R *.dfm}procedure InitTree(TreeNodes:TTreeNodes;ParentNode:TTreeNode;Path:AnsiString);
    var
      wfd:WIN32_FIND_DATA;
      hdl:THandle;
      Node:TTreeNode;
    begin
      Windows.ZeroMemory(@wfd,sizeof(WIN32_FIND_DATA));
      hdl := Windows.FindFirstFile(PAnsiChar(Path + '*.*'),wfd);
      if hdl <> INVALID_HANDLE_VALUE then
      begin
        //添加找到的第一个文件
        if (AnsiString(wfd.cFileName) <> '.') and (AnsiString(wfd.cFileName) <> '..') then
        begin
          Node := TreeNodes.AddChild(ParentNode,wfd.cFileName);
          //如果是目录,则递归添加子目录
          if (wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY then
          begin
            InitTree(TreeNodes,Node,Path + wfd.cFileName + '\');
          end;
        end;    Windows.ZeroMemory(@wfd,sizeof(WIN32_FIND_DATA));
        while Windows.FindNextFile(hdl,wfd) do
        begin
          //添加找到的文件
          if (AnsiString(wfd.cFileName) <> '.') and (AnsiString(wfd.cFileName) <> '..') then
          begin
            Node := TreeNodes.AddChild(ParentNode,wfd.cFileName);
            //如果是目录,则递归添加子目录
            if (wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY then
            begin
              InitTree(TreeNodes,Node,Path + wfd.cFileName + '\');
            end;
          end;      Windows.ZeroMemory(@wfd,sizeof(WIN32_FIND_DATA));
        end;
        Windows.FindClose(hdl);
      end;
    end;procedure TMainForm.btnInitClick(Sender: TObject);
    var
      Path:AnsiString;
      Node:TTreeNode;
    begin
      TV.Items.Clear;  Path := edtPath.Text;
      if (Path <> '') and (DirectoryExists(Path)) then
      begin
        if not IsPathDelimiter(Path,Length(Path)) then
        begin
          Path := Path + '\';
        end;
        Node := TV.Items.AddChild(NIL,Copy(Path,1,Length(Path) - 1));
        InitTree(TV.Items,Node,Path);
      end;
    end;procedure TMainForm.btnSelectDirClick(Sender: TObject);
    var
      bi:BROWSEINFO;
      Path:array[0..MAX_PATH] of char;
      il:PITEMIDLIST;
    begin
      Windows.ZeroMemory(@bi,sizeof(BROWSEINFO));
      bi.hwndOwner := Self.Handle;
      bi.pidlRoot := NIL;
      bi.lpszTitle := '请选择一个目录';
      bi.pszDisplayName := Path;
      bi.lpfn := NIL;
      bi.lParam := 0;
      bi.iImage := 0;
      il := SHBrowseForFolder(bi);
      if il <> NIL then
      begin
        if SHGetPathFromIDList(il,Path) then
        begin
          edtPath.Text := Path;
        end;
      end;
    end;end.
      

  3.   

    http://blog.csdn.net/GARNETT2183
    ////////////////
    文件历遍