Search code examples
delphiopendialog

Delphi - how to get a list of all files of directory


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.


Solution

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