Link to home
Start Free TrialLog in
Avatar of mathes
mathes

asked on

remembering the work directory

Hi experts,

I coded an application which contains a dialogue which prompts the user for a filename where the new input will be stored. My problem is that my program by default always suggests the same specific directory where the files will be stored. This can be very annoying for the user, if he wants to choose another directory for his personal files. He always has to jump from the default directory to his desired directory. And if the user saves 10 files during one session, he always has to browse through many folders....

Well, you will agree with me that this situation is not very user friendly.

Can you please tell me an elegant way how I can remember the preferred directory during one session?

Avatar of Oli2
Oli2

save the desired path to a ini file and set the directory before launching the open dialog

var ini: TiniFile;
    path: String;
begin
  ini := TInifile.Create('setting.ini');
  path :=  ini.ReadString('SETTINGS','PATH','');
  ini.Free;
  OpenDialog1.InitialDir := path;
  if OpenDialog1.Execute then
     begin
       //your code here
     end;
end;

Regards,
Oli
even better:

var ini: TiniFile;
   path: String;
begin
 ini := TInifile.Create('setting.ini');
 path :=  ini.ReadString('SETTINGS','PATH','');
 SaveDialog1.InitialDir := path;
 if SaveDialog1.Execute then
    begin
      ini.WriteString('SETTINGS','PATH',ExtractFilePath(SaveDialog1.FileName));
      ini.Free;
      //your code here
    end else
        ini.Free;
end;

This way, you make sure the entry in the ini file is always updated...

Cheers,
Oli
it's easier to use registry Tregistry component
Avatar of mathes

ASKER

Hi experts,

thank you so far for your input. Your suggestions are interesting, but they have one downside: This solution in my opinion is not very flexible. The preferred dirctory may change from day to day, or even from session to session, within a few hours or minutes. The ide with the ini file only works perfectly if the work directory is always the same.

Is it possible to implement a "temporary" solution, where the work dir is determined during runtime?
 
you can change the inifile any time
In my second example code,
the last-used path gets stored and used next time when the SaveDialog is executed.

So I don't get your point "if the work directory is always the same" at all... Did I miss something?

I don't really agree in "using TRegistry is easier".
It sure needs more lines of code and if the user deletes the program, the info stays inside the registry forever.

Regards,
Oli
I think, perhaps, that mathes may be looking for a sort of "disposable" dir the he needs to remember for a bit.  I don't have access to the help files here, but I know there's a getTempFileName function - there's prob. a getTempDirectoryName, or something similar, too.

OTOH, a quick search of the msdn does not reveal such a function.  You could, however, call getTempFileName, delete the resulting file - keeping the fileName, and then create a dir of that name.

GL
Mike
Avatar of Mohammed Nasman
Hello

  try to use HistoryList, to open last folder you were in


procedure TForm1.Button1Click(Sender: TObject);
begin
  SaveDialog1.InitialDir := SaveDialog1.HistoryList.CommaText;
  if SaveDialog1.Execute then
  begin
    // place ur code here
  end;
end;

Best regards
Mohammed Nasman
I am using Delphi 3 and just written a test program with one savedialog and one button, the following is my code for button's onclick event.

procedure TForm1.Button1Click(Sender: TObject);
begin
   If SaveDialog1.Execute Then
   Begin
      //your code here.
   End;
end;

It works just fine to remember the last folder I selected, no special code needed. So I think you maybe set the SaveDialog1.InitialDir path incorrectly in your code.
Sounds to me like it would be nice if you had your program store the last
few directories that were used for storage in a list and when the user wants
to save a new file show him the list so he can either choose from the list or
pick a new dir...if he picks a new one then put it onto the top of the list and drop
the last dir from the list (so it does not get too long)  ... save the list to an ini
file or store it inside the exe.....no more browsing through directories and no
forgetting where you stored things yesterday or the day before :-)
ASKER CERTIFIED SOLUTION
Avatar of sburck
sburck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mathes

ASKER

Hi experts,

thank you all for your input. The solution of sburck fits best to my needs.
I'm sorry to say, but my solution is better,
because it does EXACTLY THE SAME as sburcks solution
(remembers and uses the last-used path) but does also store this information so it's still available after a restart of the program.

sburck's solution will loose the InitialDir information once the program is terminated.

I think the problem is you didn't understand at all what my code is doing...

Regards,
Oli