-1

I need to know how to set the initial directory using the example from this link.

TFileOpenDialog in FireMonkey Application

Here is the code I am using, It works fine other than being able to set the initial folder.

function WinVistaSelectFolder: String; 
var
  FileDialog: IFileDialog;
  hr: HRESULT;
  IResult: IShellItem;
  initfldr:ishellitem;
  FileName: PWideChar;
  Settings: Cardinal;
  Title: String;
begin
  hr := CoCreateInstance (CLSID_FileOpenDialog,nil,CLSCTX_INPROC_SERVER,IFileDialog, FileDialog);
  if hr = S_OK then
  begin
    FileDialog.GetOptions(Settings);
    Settings := Settings or FOS_PICKFOLDERS or FOS_FORCEFILESYSTEM;
    FileDialog.SetOptions(Settings);
    FileDialog.SetOkButtonLabel('Select');
     Title := 'Select a directory';
    FileDialog.SetTitle(PWideChar(Title));
    hr := FileDialog.Show(0);
    if hr = S_OK then
    begin
      hr := FileDialog.GetResult(IResult);
      if hr = S_OK then
      begin
        IResult.GetDisplayName(SIGDN_FILESYSPATH, FileName);
        Result := FileName;
      end;
    end;
  end;
end;
Community
  • 1
  • 1
Lewis Harris
  • 15
  • 1
  • 7

2 Answers2

1

Assuming that you are using the TFileOpenDialog component, then you need to set the DefaultFolder property.

If in fact you are using IFileDialog rather than TFileOpenDialog. Then you should use the SetDefaultFolder method. Alternatively, depending on your precise needs, SetFolder may be appropriate.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • in FM there is no TFileOpenDialog only TOpenDialog. and TOpenDialog does not have a default folder property. – Lewis Harris Nov 30 '15 at 23:53
  • The question you linked to asks about using TFileOpenDialog in FMX which is possible. Perhaps you should take some time to ask a clearer question. Is your question just how to use IFileDialog? I note that you've not included error checking in your code. – David Heffernan Dec 01 '15 at 07:02
-3

Use GetCurrentDir to find out what it is, or SetCurrentDir to change it. Both are found in the System.IOUtils namespace on the TDirectory class.

Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194
  • 1
    No. That's the process working directory. The question relates to the initial directory for the dialog. – David Heffernan Nov 24 '15 at 06:40
  • Also, System.IOUtils is a unit rather than a namespace. – David Heffernan Nov 24 '15 at 07:19
  • @DavidHeffernan Units are Namespaces. If you don't otherwise specify the dialog to go to a specific directory then it uses the process working directory. – Jim McKeeth Nov 24 '15 at 13:41
  • Not so. From the [documentation](http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_Namespaces_with_Delphi), *In Delphi, a unit is the basic container for types. In Delphi, a namespace is a container of Delphi units.* – David Heffernan Nov 24 '15 at 13:46
  • As for initial directory you are wrong there too. It's much more complex than that. There are many nuances. For instance, the system remembers which directory an application last opened a file from. Furthermore, even if what you were saying were true, changing the process working directory to specify the initial dir of a file dialog is a ludicrous idea. Imagine the consequences and side effects if that were the way to do it. – David Heffernan Nov 24 '15 at 13:47
  • 1
    Drop a `TFileOpenDialog` onto a form and have its Execute method run. You claim that the dialog will open in the process working directory. It does not. Since your answer has a great many problems I think it would be best for you to delete it. Considering that you are an Embarcadero employee its not really very good marketing for an Employee to be so far wide of the mark. – David Heffernan Nov 24 '15 at 13:49