Changing the Delphi OpenDialog Directory

Small background of the program:
The program uses a tabbed interface for simultaneous work with several files.
I'm trying to change the OpenDialog directory, so every time I call an open file, the directory of the file I'm currently working on will be displayed, but even when I set InitialDirthe file path, it always displays the last open file directory, not the one which I install for him.
I tried the following:

if Length(CurrentFileName) > 0 then
begin
  OpenFileDialog.InitialDir :='';
  SetCurrentDirectory(PChar(CurrentFileName));
  OpenFileDialog.InitialDir := ExtractFileDir(CurrentFileName);
end;
if OpenFileDialog.Execute then
...

Where CurrentFileNameis the full path with the file name of the currently opened tab file. But no luck.

Is there any way to achieve this?

So for example:

tab1open c:\mydir\file.txt tab2opend:\someotherdir\somefile.txt

tab1 I, OpenDialog c:\mydir\

Delphi 7. .

+5
5

,

if Length(CurrentFileName) > 0 then
  OpenFileDialog.FileName := ExtractFilePath(CurrentFileName);

if OpenFileDialog.Execute

, , -, , .

, ,

  OpenFileDialog.FileName := ExtractFilePath(CurrentFileName);
  OpenFileDialog.InitialDir := OpenFileDialog.FileName;
  SetCurrentDirectory(PChar(OpenFileDialog.FileName));

. ! over-kill, Windows , .

, . SetCurrentDirectory , .

+5

Windows Vista :

  • lpstrFile , .
  • lpstrInitialDir .
  • , "" " " , . , , .
  • lpstrInitialDir NULL, , - .
  • - .
  • - " ".

, FileName , InitialDir , . , , , FileName .

( Windows 7, . , , , - .)

+8

FileName , :

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDialog1.FileName:= '';
  OpenDialog1.InitialDir:= 'C:\';
  OpenDialog1.Execute;
end;
+3

- Opendialog1.InitialDir := GetCurrentDir - . Opendialog1.Title := Opendialog1.InitialDir - . ​​ (dir)

dirr := GetCurrentDir;
Opendialog1.InitialDir := dirr;

Everything seems to be working fine (not sure why, but working) HTH

+1
source

It's just tested, and the code below works in Delphi 2010. Either this is a bug in Delphi 7, or you need to double check that the value of CurrentFileName is what you intend to do.

procedure TForm1.Button1Click(Sender: TObject);
begin
  opendialog1.InitialDir := 'c:\temp';
  opendialog1.Execute;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  opendialog1.InitialDir := 'c:\temp\fpc';
  opendialog1.Execute;
end;
0
source

All Articles