• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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?

0
mathes
Asked:
mathes
  • 4
  • 2
  • 2
  • +5
1 Solution
 
Oli2Commented:
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
0
 
Oli2Commented:
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
0
 
f15iafCommented:
it's easier to use registry Tregistry component
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
mathesAuthor Commented:
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?
 
0
 
f15iafCommented:
you can change the inifile any time
0
 
Oli2Commented:
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
0
 
edeyCommented:
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
0
 
Mohammed NasmanSoftware DeveloperCommented:
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
0
 
Richard_YuCommented:
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.
0
 
GwenaCommented:
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 :-)
0
 
sburckCommented:
If you are talking about the standard savedialog and opendialog, after the user has made his selection, do this, for example:

if (SaveDialog1.Execute) then
begin
     Selection := SaveDialog1.FileName;
     SaveDialog1.InitialDir := ExtractFilePath(Selection);
end;

Next time you use SaveDialog, it's in the same place.

0
 
mathesAuthor Commented:
Hi experts,

thank you all for your input. The solution of sburck fits best to my needs.
0
 
Oli2Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now