GetDir in Delphi2010 not working under Windows 7?

690 views Asked by At

I have the following sequence of commands in Delphi2010:

  var netdir:string;
  ....
  OpenDialog1.InitialDir:=netdir;
  ....
  OpenDialog1.Execute...
  ....
  GetDir(0,netdir);
  ....

After executing OpenDialog I should have in string netdir the directory where I finished my OpenDialog.Execute. And in the next OpenDialog.Execute it should start from that directory. It works fine on XP, but not on Windows 7? It always starts from directory where the program is installed.

Any idea what might be wrong?

Thanks.

3

There are 3 answers

0
Ilkka On

If you not wan´t opendialog you can do as below to get dir under your program.

yourdir:=ExtractFilePath(Application.ExeName);

I have done it in Vista and it work.

0
user2567682 On

This is the solution for the problem

openDialog1.Options := [ofFileMustExist];

if openDialog1.Execute then
begin

end;
0
Andreas Rejbrand On

Your question cannot be answered as it stands, because it lacks several crucial details.

  1. Is netdir a global constant, or does it go out of scope every now and then?
  2. Do you set netdir to something prior to OpenDialog1.Execute?
  3. Is the question about what directory GetDir return (as your title suggests), or about how to make the open dialog remember the last visited directory (as the body matter suggests)?

I will assume that 1) netdir is a global constant, that 2) you do not set it initially, and that 3) you want the open dialog to remember the last visited folder. Thus you have something like

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm3 = class(TForm)
    OpenDialog1: TOpenDialog;
    procedure FormClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

var
  netdir: string;

implementation

{$R *.dfm}

procedure TForm3.FormClick(Sender: TObject);
begin
  OpenDialog1.InitialDir := netdir;
  OpenDialog1.Execute;
  GetDir(0, netdir);
end;

end.

Then the solution is to let Windows remember the directory for you, that is, simply do

procedure TForm3.FormClick(Sender: TObject);
begin
  OpenDialog1.Execute;
end;

alone! But why doesn't your method work? Well, GetDir doesn't return what you want. If you need explicit control, do

procedure TForm3.FormClick(Sender: TObject);
begin
  OpenDialog1.InitialDir := netdir;
  OpenDialog1.Execute;
  netdir := ExtractFilePath(OpenDialog1.FileName)
end;