Solved

remembering the work directory

Posted on 2001-06-08
13
208 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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:mnasman
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

11 Experts available now in Live!

Get 1:1 Help Now