Solved

remembering the work directory

Posted on 2001-06-08
13
212 Views
Last Modified: 2010-04-06
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
Comment
Question by:mathes
  • 4
  • 2
  • 2
  • +5
13 Comments
 
LVL 1

Expert Comment

by:Oli2
ID: 6169658
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
 
LVL 1

Expert Comment

by:Oli2
ID: 6169665
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
 
LVL 3

Expert Comment

by:f15iaf
ID: 6169712
it's easier to use registry Tregistry component
0
 

Author Comment

by:mathes
ID: 6169760
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
 
LVL 3

Expert Comment

by:f15iaf
ID: 6169763
you can change the inifile any time
0
 
LVL 1

Expert Comment

by:Oli2
ID: 6169823
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:edey
ID: 6170231
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
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6170463
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
 

Expert Comment

by:Richard_Yu
ID: 6170746
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
 
LVL 5

Expert Comment

by:Gwena
ID: 6171706
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
 
LVL 3

Accepted Solution

by:
sburck earned 100 total points
ID: 6174289
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
 

Author Comment

by:mathes
ID: 6177643
Hi experts,

thank you all for your input. The solution of sburck fits best to my needs.
0
 
LVL 1

Expert Comment

by:Oli2
ID: 6177711
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now