各位高手:請教在delphi中怎樣根據知道的路徑去找該路徑上的文件名呢?急!急!

解决方案 »

  1.   

    form1中有edit1写入路径。listbox显示文件。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls, FileCtrl, ComCtrls;type
      TForm1 = class(TForm)
        Panel1: TPanel;
        Label1: TLabel;
        Edit1: TEdit;
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Edit1KeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        FNewPath: string;
        procedure DoSearchPathFile(qPath: string);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
      ListBox1.Items.Clear;
      DoSearchPathFile(Edit1.Text);end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Close;
    end;procedure TForm1.DoSearchPathFile(qPath: string);
    var
      SR: TSearchRec;
      FileAttr: Integer;
    begin
      FileAttr := faDirectory;
      FindFirst(qPath  + '\*.*', FileAttr, SR);  While FindNext(SR) = 0 do
      begin
        if (SR.Name <> '.') and (SR.Name <> '..') then begin
          if DirectoryExists(qPath + '\' + SR.Name) then
          begin
            ListBox1.Items.Add('路径'+ qPath + '\' + SR.Name);
            DoSearchPathFile(qPath + '\' + SR.Name);      end;
          //;else ListBox1.Items.Add(qPath + '\' + SR.Name);    end;
      end;  FileAttr := faAnyFile;  FindFirst(qPath + '\*.*', FileAttr, SR);
      while FindNext(SR) = 0 do
      begin
        if (SR.Name <> '.') and (Sr.Name <> '..') then
        begin
          if not(DirectoryExists(qPath + '\' + SR.Name)) then
            ListBox1.Items.Add(qPath + '\' + SR.Name);
        end;
      end;
    end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin  if key = vk_Return then
      begin
        if Trim(Edit1.Text) = '' then
        begin
          ShowMessage('Invalid PathName');
          Exit;
        end;    FNewPath := Edit1.Text[Length(Edit1.Text)];
        if FNewPath = '\' then
          FNewPath := Copy(Edit1.Text, 1, Length(Edit1.Text) - 1)
        else
        begin
          FNewPath := Edit1.Text;
        end;    ListBox1.Clear;
        DoSearchPathFile(FNewPath);  end;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      self.ListBox1.Items.SaveToFile('FileList.txt');
    end;end.