Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

remembering the work directory

Posted on 2001-06-08
13
Medium Priority
?
250 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

926 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