SaveDialog / OpenDialog default directory path

Hi,
Does anyone know how to get the directory path that an OpenDialog or SaveDialog box will open to (assuming an unspecified InitialDir)?

It can be extracted from the Filename property once the dialog has been executed, but I want to find the directory path that it will be opening to before my app executes it.

ie. I have a box that displays the last directory that a file was opened from or saved to (the user's working dir). It's all well and good after a dialog execution but I want to be able to put something in there when the application form is Created (without forcing the InitialDir to some hardcoded directory).

Thanks

Comments

  • : Hi,
    : Does anyone know how to get the directory path that an OpenDialog or SaveDialog box will open to (assuming an unspecified InitialDir)?
    :
    : It can be extracted from the Filename property once the dialog has been executed, but I want to find the directory path that it will be opening to before my app executes it.
    :
    : ie. I have a box that displays the last directory that a file was opened from or saved to (the user's working dir). It's all well and good after a dialog execution but I want to be able to put something in there when the application form is Created (without forcing the InitialDir to some hardcoded directory).
    :
    : Thanks
    :
    :
    The current directory can be retrieved using the GetCurrentDirectory() function (see help files for more info).
    As for storing the dialog's directory, I suggest you use a TIniFile key or a TRegistry key to store the working directory in. Here is a code based on the TIniFile to help you get started.
    [code]
    procedure Form1.FormCreate(Sender: TObject);
    var
    IniFile: TIniFile;
    begin
    IniFile := TIniFile.Create('MyApp.ini');
    with IniFile do try
    OpenDialog1.InitialDir := ReadString('Paths', 'WorkDir',
    ExtractFilePath(Application.ExeName));
    // Get the InitialDir from the IniFile or set it to the application dir
    finally
    IniFile.Free;
    end;
    end;

    procedure Form1.FormDestroy(Sender: TObject);
    var
    IniFile: TIniFile;
    begin
    IniFile := TIniFile.Create('MyApp.ini');
    with IniFile do try
    if OpenDialog1.Filename <> '' then
    WriteString('Paths', 'WorkDir', ExtractFilePath(OpenDialog1.Filename));
    // Write the file path to the inifile for next time, but only if the opendialog was executed.
    finally
    IniFile.Free;
    end;
    end;
    [/code]
    The initial directory is hardcoded into the program, but after the program has run and the opendialog's directory has changed the new directory is recorded. The second time the program runs, the initialdir is set to the stored directory.
  • : : Hi,
    : : Does anyone know how to get the directory path that an OpenDialog or SaveDialog box will open to (assuming an unspecified InitialDir)?
    : :
    : : It can be extracted from the Filename property once the dialog has been executed, but I want to find the directory path that it will be opening to before my app executes it.
    : :
    : : ie. I have a box that displays the last directory that a file was opened from or saved to (the user's working dir). It's all well and good after a dialog execution but I want to be able to put something in there when the application form is Created (without forcing the InitialDir to some hardcoded directory).
    : :
    : : Thanks
    : :
    : :
    : The current directory can be retrieved using the GetCurrentDirectory() function (see help files for more info).
    : As for storing the dialog's directory, I suggest you use a TIniFile key or a TRegistry key to store the working directory in. Here is a code based on the TIniFile to help you get started.
    : [code]
    : procedure Form1.FormCreate(Sender: TObject);
    : var
    : IniFile: TIniFile;
    : begin
    : IniFile := TIniFile.Create('MyApp.ini');
    : with IniFile do try
    : OpenDialog1.InitialDir := ReadString('Paths', 'WorkDir',
    : ExtractFilePath(Application.ExeName));
    : // Get the InitialDir from the IniFile or set it to the application dir
    : finally
    : IniFile.Free;
    : end;
    : end;
    :
    : procedure Form1.FormDestroy(Sender: TObject);
    : var
    : IniFile: TIniFile;
    : begin
    : IniFile := TIniFile.Create('MyApp.ini');
    : with IniFile do try
    : if OpenDialog1.Filename <> '' then
    : WriteString('Paths', 'WorkDir', ExtractFilePath(OpenDialog1.Filename));
    : // Write the file path to the inifile for next time, but only if the opendialog was executed.
    : finally
    : IniFile.Free;
    : end;
    : end;
    : [/code]
    : The initial directory is hardcoded into the program, but after the program has run and the opendialog's directory has changed the new directory is recorded. The second time the program runs, the initialdir is set to the stored directory.
    :


    I'm already using a config file for initialization so adding the dir to it would be easy and should be good enough.
    I'm using GetDir to get the home path of the program. The problem with GetDir and GetCurrentDirectory is that they return the path of the exe (until a dialog box is actually executed). The dialog box execution defaults to the last directory that a file was opened from or saved to (which is often a different directory than the homepath) and it is THIS directory that I want to extract. It doesn't appear to be saved in the dialog properties or in the registry. I have no idea where it could be keeping it.
    Anyway, thanks for the suggestion.

  • : : : Hi,
    : : : Does anyone know how to get the directory path that an OpenDialog or SaveDialog box will open to (assuming an unspecified InitialDir)?
    : : :
    : : : It can be extracted from the Filename property once the dialog has been executed, but I want to find the directory path that it will be opening to before my app executes it.
    : : :
    : : : ie. I have a box that displays the last directory that a file was opened from or saved to (the user's working dir). It's all well and good after a dialog execution but I want to be able to put something in there when the application form is Created (without forcing the InitialDir to some hardcoded directory).
    : : :
    : : : Thanks
    : : :
    : : :
    : : The current directory can be retrieved using the GetCurrentDirectory() function (see help files for more info).
    : : As for storing the dialog's directory, I suggest you use a TIniFile key or a TRegistry key to store the working directory in. Here is a code based on the TIniFile to help you get started.
    : : [code]
    : : procedure Form1.FormCreate(Sender: TObject);
    : : var
    : : IniFile: TIniFile;
    : : begin
    : : IniFile := TIniFile.Create('MyApp.ini');
    : : with IniFile do try
    : : OpenDialog1.InitialDir := ReadString('Paths', 'WorkDir',
    : : ExtractFilePath(Application.ExeName));
    : : // Get the InitialDir from the IniFile or set it to the application dir
    : : finally
    : : IniFile.Free;
    : : end;
    : : end;
    : :
    : : procedure Form1.FormDestroy(Sender: TObject);
    : : var
    : : IniFile: TIniFile;
    : : begin
    : : IniFile := TIniFile.Create('MyApp.ini');
    : : with IniFile do try
    : : if OpenDialog1.Filename <> '' then
    : : WriteString('Paths', 'WorkDir', ExtractFilePath(OpenDialog1.Filename));
    : : // Write the file path to the inifile for next time, but only if the opendialog was executed.
    : : finally
    : : IniFile.Free;
    : : end;
    : : end;
    : : [/code]
    : : The initial directory is hardcoded into the program, but after the program has run and the opendialog's directory has changed the new directory is recorded. The second time the program runs, the initialdir is set to the stored directory.
    : :
    :
    :
    : I'm already using a config file for initialization so adding the dir to it would be easy and should be good enough.
    : I'm using GetDir to get the home path of the program. The problem with GetDir and GetCurrentDirectory is that they return the path of the exe (until a dialog box is actually executed). The dialog box execution defaults to the last directory that a file was opened from or saved to (which is often a different directory than the homepath) and it is THIS directory that I want to extract. It doesn't appear to be saved in the dialog properties or in the registry. I have no idea where it could be keeping it.
    : Anyway, thanks for the suggestion.
    :
    :
    After the first time the dialog box is executed the directory it was opened to is kept in the memory space of your program. After your program closes that data is deallocated and thus forgotten. It is your task to create a more permanent record of that data.
  • : : : : Hi,
    : : : : Does anyone know how to get the directory path that an OpenDialog or SaveDialog box will open to (assuming an unspecified InitialDir)?
    : : : :
    : : : : It can be extracted from the Filename property once the dialog has been executed, but I want to find the directory path that it will be opening to before my app executes it.
    : : : :
    : : : : ie. I have a box that displays the last directory that a file was opened from or saved to (the user's working dir). It's all well and good after a dialog execution but I want to be able to put something in there when the application form is Created (without forcing the InitialDir to some hardcoded directory).
    : : : :
    : : : : Thanks
    : : : :
    : : : :
    : : : The current directory can be retrieved using the GetCurrentDirectory() function (see help files for more info).
    : : : As for storing the dialog's directory, I suggest you use a TIniFile key or a TRegistry key to store the working directory in. Here is a code based on the TIniFile to help you get started.
    : : : [code]
    : : : procedure Form1.FormCreate(Sender: TObject);
    : : : var
    : : : IniFile: TIniFile;
    : : : begin
    : : : IniFile := TIniFile.Create('MyApp.ini');
    : : : with IniFile do try
    : : : OpenDialog1.InitialDir := ReadString('Paths', 'WorkDir',
    : : : ExtractFilePath(Application.ExeName));
    : : : // Get the InitialDir from the IniFile or set it to the application dir
    : : : finally
    : : : IniFile.Free;
    : : : end;
    : : : end;
    : : :
    : : : procedure Form1.FormDestroy(Sender: TObject);
    : : : var
    : : : IniFile: TIniFile;
    : : : begin
    : : : IniFile := TIniFile.Create('MyApp.ini');
    : : : with IniFile do try
    : : : if OpenDialog1.Filename <> '' then
    : : : WriteString('Paths', 'WorkDir', ExtractFilePath(OpenDialog1.Filename));
    : : : // Write the file path to the inifile for next time, but only if the opendialog was executed.
    : : : finally
    : : : IniFile.Free;
    : : : end;
    : : : end;
    : : : [/code]
    : : : The initial directory is hardcoded into the program, but after the program has run and the opendialog's directory has changed the new directory is recorded. The second time the program runs, the initialdir is set to the stored directory.
    : : :
    : :
    : :
    : : I'm already using a config file for initialization so adding the dir to it would be easy and should be good enough.
    : : I'm using GetDir to get the home path of the program. The problem with GetDir and GetCurrentDirectory is that they return the path of the exe (until a dialog box is actually executed). The dialog box execution defaults to the last directory that a file was opened from or saved to (which is often a different directory than the homepath) and it is THIS directory that I want to extract. It doesn't appear to be saved in the dialog properties or in the registry. I have no idea where it could be keeping it.
    : : Anyway, thanks for the suggestion.
    : :
    : :
    : After the first time the dialog box is executed the directory it was opened to is kept in the memory space of your program. After your program closes that data is deallocated and thus forgotten. It is your task to create a more permanent record of that data.
    :

    It's definitely good programming practice to not rely on Windows whatsoever, so I did stick the dir in my config file to control it myself. As an FYI, once the memory space is gone on Windows 95 the directory is gone with it, but Windows 2000 and XP both seem to attach the last directory to a specific .exe name even after the program is closed and it's memory space destroyed. I develop on 2000 and I can copy my exe anywhere and the Dialogs still open to the last dir opened. If I rename the exe to an original name, the dialog dir reverts back to the original default. It also keeps the dir after rebooting so Windows definitely has it stashed somewhere outside of RAM.


  • windows 7 seems to store MRU cache by application exe name and maybe filename filters

    HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32

    But how to interprete those semi-binary keys i do not know

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories