markus_ja
markus_ja

Reputation: 2951

Changing the directory of Delphi OpenDialog + Drive in Win7

It seems that on Win7 changing the TOpenDialog.InitialDir doesn't work, when the new directory is on a different drive, than the current directory.

e.g.: I want to change my InitialDir from 'C:\program files\MyApp' to 'D:\test\MyAppData'

Is that a known issue, or only on my computer?

I already tried the same thing, as mentioned in the following post, but without any success: Changing the directory of Delphi OpenDialog

EDIT: I am using DelphiXE on Win7 32 Bit

The path/dir is correct: So, when I copy that path from code and past it into the 'File Name' field of that Dialog itself and I press ENTER, then the Dialog switches to that directory. Only, in my code it is not working.

UPDATE:
I found the problem. If the path contains some path commands like ..\ the TOpenDialog.InitialDir is not able to resolve that. Use TPath.GetFullPath(...) to make it clean.

Upvotes: 4

Views: 2312

Answers (2)

ferpega
ferpega

Reputation: 3224

I have tested on a Delphi XE, it runs fine... I have done this:

Put a new form:

object Form4: TForm4
  Left = 0
  Top = 0
  Caption = 'Form4'
  ClientHeight = 204
  ClientWidth = 447
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 24
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 120
    Top = 42
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'D:\'
  end
  object OpenDialog1: TOpenDialog
    InitialDir = 'C:\'
    Left = 120
    Top = 72
  end
end

And its source code:

unit Unit4;

interface

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

type
  TForm4 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);
begin

  OpenDialog1.InitialDir := edit1.text;
  OpenDialog1.Execute;
end;

end.

Regards

Upvotes: 2

MGH
MGH

Reputation: 1189

I don't have any problem changing InitialDir, either through object inspector or runtime (Win7 with Delphi 2010). Try doublechecking if the directory you try to change to is correctly typed.

Upvotes: 1

Related Questions