views:

306

answers:

4

Hello all...
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.

+1  A: 

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. Perhaps this is what you seek.

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

Jens Björnhager
@Jens : oh no... You get it wrong.... Please have a look at edited question.
Himadri
@Jens : I am not selecting a directory but a file.. and I want other file list of parent directory of selected file.. read the question carefully...
Himadri
In that case, CaldonCZE has the answer.
Jens Björnhager
+1  A: 

You can use extractFilePath function to get the directory name:

myPath := extractFilePath(FileName);

where FileName is name of file you choose by OpenDialog.

Ondra C.
+5  A: 

@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('*.*', 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;
RRUZ
@RRUZ : OK. thanx I got it. Your answer helps me.
Himadri
I think your code needs a try finally protecting FindFirst/FindClose(SR).
Warren P
+3  A: 

if you use delphi 2010 then you can use tdirectory.getfiles first add ioutils.pas to uses clause then write the following line of code in the event handler(in addition to code you already have in that event handler)

 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;
Omair Iqbal
@omair Good option but I am not using delphi 2010... :-(
Himadri
+1 for representing with the new "for" syntax. :-)
Warren P