簡體   English   中英

Delphi - 如何獲取目錄中所有文件的列表

[英]Delphi - how to get a list of all files of directory

我正在使用 delphi,當我執行 openpicturedialog 時,我想要一個目錄的所有文件的列表。

即,當執行打開對話框並從中選擇一個文件時,我想要所選文件目錄中所有文件的列表。

你甚至可以建議我從TOpenDialog FileName屬性中獲取目錄名
謝謝。

如果您使用 delphi 2010,那么您可以使用 tdirectory.getfiles 首先將 ioutils.pas 添加到 uses 子句,然后在事件處理程序中編寫以下代碼行(除了您在該事件處理程序中已有的代碼之外)

uses IOUtils;

 var
    path : string;
begin
    for Path in TDirectory.GetFiles(OpenPictureDialog1.filename)  do
        Listbox1.Items.Add(Path);{assuming OpenPictureDialog1 is the name you gave to your OpenPictureDialog control}
end;

@Himadri,OpenPictureDialog 的主要目標不是選擇目錄,無論如何,如果您將此對話框用於其他目的,您可以嘗試此代碼。

Var
  Path    : String;
  SR      : TSearchRec;
  DirList : TStrings;
begin
  if OpenPictureDialog1.Execute then
  begin
    Path:=ExtractFileDir(OpenPictureDialog1.FileName); //Get the path of the selected file
    DirList:=TStringList.Create;
    try
          if FindFirst(Path + '*.*', faArchive, SR) = 0 then
          begin
            repeat
                DirList.Add(SR.Name); //Fill the list
            until FindNext(SR) <> 0;
            FindClose(SR);
          end;

     //do your stuff

    finally
     DirList.Free;
    end;
  end;

end;

更改 OpenPictureDialog 中的過濾器屬性以包含所有文件:

All (*.*)

編輯:我認為您不能在 Open(Picture)Dialog 中選擇一個目錄,無論如何它肯定不是 OpenPictureDialog 的目的。

然后使用FindFirstFindNext獲取此目錄中的文件。

您可以使用 extractFilePath 函數來獲取目錄名稱:

myPath := extractFilePath(FileName);

其中 FileName 是您通過 OpenDialog 選擇的文件名。

if OpenPictureDialog1.Execute then  
  FileListBox1.Directory := extractFilePath(OpenPictureDialog1.FileName);

您還可以使用鏈接到 FileListBox 的 FilterComboBox 來過濾文件類型。

TFileListBox 和 TFilterComboBox 位於“Win 3.1”下的工具面板中。 Delphi 4 中有這些對象。

使用 System.IOUtils;

變量列表:TStringlist; var 文件:字符串:= ''; enter code here var Path : string := IncludeTrailingPathDelimiter(Edit1.Text);

列表:= TStringList.Create; 嘗試為 TDirectory.GetFiles(Path) 中的 File 做 List.Add(File); // 最后將所有文件名添加到列表中 FreeAndNil(Lista); 結尾;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM