Delphi-如何获取目录中所有文件的列表

发布于 2024-09-05 08:15:49 字数 236 浏览 0 评论 0原文

我正在使用 delphi,当我执行 openpicturedialog 时,我想要一个目录中所有文件的列表。

即,当执行打开对话框时并且 我从中选择一个文件,我想要 目录中所有文件的列表 所选文件的名称。

您甚至可以建议我从 TOpenDialogFileName 属性获取目录名称
谢谢。

I am working with delphi, I want a list of all files of a directory when I execute openpicturedialog.

i.e., When open dialog is executed and
i select one file from it, I want the
list of all files from the directory
of selected file.

You can even suggest me for getting directory name from FileName property of TOpenDialog
Thank You.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

丘比特射中我 2024-09-12 08:15:49

如果您使用的是 Delphi 2010,则可以使用 TDirectory.GetFiles

首先将 IOUtils 添加到您的 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;

If you're using Delphi 2010 then you can use TDirectory.GetFiles.

First add IOUtils to your uses clause, then use the following in your event handler (in addition to code you already have in that event handler):

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;
酷遇一生 2024-09-12 08:15:49

@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;

@Himadri, the primary objective of the OpenPictureDialog is not select an directory, anyway if you are using this dialog with another purpose you can try this code.

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;
朱染 2024-09-12 08:15:49

更改 OpenPictureDialog 中的 filter 属性以包含所有文件:

All (*.*)

编辑:我认为您不能在 Open(Picture)Dialog 中选择目录,它肯定不能无论如何,这不是 OpenPictureDialog 的目的。

然后使用 FindFirstFindNext 获取此目录中的文件。

Change the filter property in your OpenPictureDialog to include all files:

All (*.*)

Edit: I don't think you can select a directory in a Open(Picture)Dialog, it surely isn't the purpose of an OpenPictureDialog anyway.

Then use FindFirst and FindNext to get the files in this dir.

少女情怀诗 2024-09-12 08:15:49

您可以使用 extractFilePath 函数获取目录名称:

myPath := extractFilePath(FileName);

其中 FileName 是您通过 OpenDialog 选择的文件的名称。

You can use extractFilePath function to get the directory name:

myPath := extractFilePath(FileName);

where FileName is name of file you choose by OpenDialog.

聆听风音 2024-09-12 08:15:49
if OpenPictureDialog1.Execute then  
  FileListBox1.Directory := extractFilePath(OpenPictureDialog1.FileName);

您还可以使用链接到 FileListBox 的 FilterComboBox 来过滤文件类型。

TFileListBox 和 TFilterComboBox 位于“Win 3.1”下的工具选项板中。从 Delphi 4 开始就有这些对象。

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

You can also use a FilterComboBox linked to FileListBox to filter the file type.

TFileListBox and TFilterComboBox are in the tool palette under "Win 3.1". From Delphi 4 there are these objects.

時窥 2024-09-12 08:15:49

通过这段代码,你可以获取你想要的文件夹中文件的“路径”信息。为此,您可以使用 Delphi 的 System.IOUtils 库。

uses 
...
 System.IOUtils;
...

var List : TStringlist;
var File : String := '';
var Path : string := IncludeTrailingPathDelimiter(Edit1.Text);

Lista := TStringList.Create;
try
    for File in TDirectory.GetFiles(Path) do
        List.Add(File); // Add all file names to list
finally
    FreeAndNil(Lista);
end;

With this code, you can get the "path" information of the files in the folder you want. You can use Delphi's System.IOUtils library for this.

uses 
...
 System.IOUtils;
...

var List : TStringlist;
var File : String := '';
var Path : string := IncludeTrailingPathDelimiter(Edit1.Text);

Lista := TStringList.Create;
try
    for File in TDirectory.GetFiles(Path) do
        List.Add(File); // Add all file names to list
finally
    FreeAndNil(Lista);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文