Changing the directory of Delphi OpenDialog
Asked Answered
B

5

6

A little background of the program:
The program uses a tabbed interface to work on multiple files at the same time.
I'm trying to change the directory of the OpenDialog so every time I call open a file will show the directory of the file I am working on currently, but even when I set InitialDir to the file's path it always displays the last opened file directory, not the one I am setting it to.
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 CurrentFileName is the full path with the filename of the current tab's opened file. But no luck.

Is there any way to achieve this?

So for example:

tab1 has c:\mydir\file.txt opened
tab2 has d:\someotherdir\somefile.txt opened

If I move to tab1 and call the open function I the OpenDialog should show me the contents of c:\mydir\

I am using Delphi 7. Any help is appreciated.

Blessington answered 25/10, 2010 at 14:46 Comment(2)
Strangely enough, your code above works well for me (Delphi 2009, Win 7).Ketchum
I ma using borland C++ which is similar... you forgot to add "\\" at the end of InitialDir (sometimes it works without it but not allways), and also sometimes helps to call OpenDialog.Update()Psychoanalysis
K
5

I thought that

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

if OpenFileDialog.Execute

was the way to go, but apparently the situation is slightly more involuted than I thought.

Anyhow, I seriously doubt that

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

will make you disappointed. Now the three chief ways of determining the dir says the same thing! A bit over-kill, but if Windows has changed its behaviour, it might be necessary.

By the way, there is a bug in your code. SetCurrentDirectory wants a directory as argument, not a file name.

Ketchum answered 25/10, 2010 at 17:7 Comment(0)
V
9

Here are the rules for Windows Vista and earlier:

  1. If lpstrFile contains a path, that path is the initial directory.
  2. Otherwise, lpstrInitialDir specifies the initial directory.
  3. Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded.
  4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
  5. Otherwise, the initial directory is the personal files directory of the current user.
  6. Otherwise, the initial directory is the Desktop folder.

So, if the current FileName property is non-empty, then the InitialDir property is ignored, as is the application's current directory. That fits with what you've observed, so make sure you're clearing the FileName property between uses of your dialog box.

(The rules are different as of Windows 7, and they may foil your plans. If you open a file from the first tab, then open a file from the second tab, another attempt to open a file while the first tab is selected might not display the first tab's directory anymore.)

Vergne answered 25/10, 2010 at 15:24 Comment(1)
I tried running the app after I cleaned FileName on windows 7 and indeed it didn't change the directory. I remained on the last opened file directory. Then I tried it on a win XP SP 3 and the same. Cleaning FileName didn't help.Blessington
K
5

I thought that

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

if OpenFileDialog.Execute

was the way to go, but apparently the situation is slightly more involuted than I thought.

Anyhow, I seriously doubt that

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

will make you disappointed. Now the three chief ways of determining the dir says the same thing! A bit over-kill, but if Windows has changed its behaviour, it might be necessary.

By the way, there is a bug in your code. SetCurrentDirectory wants a directory as argument, not a file name.

Ketchum answered 25/10, 2010 at 17:7 Comment(0)
A
3

FileName property should be cleared, ex:

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDialog1.FileName:= '';
  OpenDialog1.InitialDir:= 'C:\';
  OpenDialog1.Execute;
end;
Apocrypha answered 25/10, 2010 at 15:23 Comment(0)
C
1

I had a similar problem - using Opendialog1.InitialDir := GetCurrentDir - didn't work. Tried displaying it as Opendialog1.Title := Opendialog1.InitialDir - showed correct string. Then declared new string (dir) and used

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

That seems to work fine (not sure why, but working) hth

Complicity answered 25/9, 2011 at 20:39 Comment(0)
G
0

Just tested and the code below works in Delphi 2010. Either it is a bug in Delphi 7 or you need to double check that the value of CurrentFileName is what you intend it to be.

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;
Glenine answered 25/10, 2010 at 15:6 Comment(3)
Did you select OK (or Open) when you opened the dialog the first time?Ketchum
I tried forcing a specific directory, as you did in this example, and still it didn't work. I doubt it's an bug on delphi 7 but I don't know...Blessington
@Andreas: I just selected the file and pressed enter. Why?Blessington

© 2022 - 2024 — McMap. All rights reserved.