Solved

How to change current dir

Posted on 2001-09-06
8
487 Views
Last Modified: 2010-04-06
After the current directory has been changed by selecting a file using an Opendialog how can I change it back to the dir where the program is.... the dir of application.exename  ?

begin
OpenDialog1.Execute;
MyStream.SaveToFile(OpenDialog1.Filename);
//now how do I set current dir back
//to the programs dir so that the following line
//saves another copy of MyStream as 'test.txt'
//in the same dir that the program is in
MyStream.SaveToFile('test.txt');
end;

0
Comment
Question by:Gwena
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Expert Comment

by:pin_plunder
ID: 6462721
// this saves the file test.txt in the dir where the executable is stored
MyStream.SaveToFile(ExtractFilePath(Application.ExeName) + 'test.txt');
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6462741
this is how you get the dir of the current executable

ExtractFilePath(Application.ExeName);

e.g.

if OpenDialog1.Execute then
begin
  MyStream.SaveToFile(OpenDialog1.FileName);
  MyStream.SaveToFile(ExtractFilePath(Application.ExeName) + 'Test.txt');
  // or if you want to save it with the same name as in OpenDialog1, but in different path:
  MyStream.SaveToFile(ExtractFilePath(Application.ExeName) + ExtractFileName(OpenDialog1.FileName));
end;


Hope that helps!



DragonSlayer.
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6462748
oops pin_plunder, I opened too many EE windows, and did not refresh 'em... hence I did not see your comments and posted mine instead... sorry! :)
0
 
LVL 5

Author Comment

by:Gwena
ID: 6462810
Hi :-)
  Yup.. I know how to save a file to the same dir as the executing program... what I'd like to know is how to set the default dir back to the dir of the exe... just as though the opendialog had never changed it at all... so that the code
MyStream.SaveToFile('test.txt');
Will save test.exe in the same dir as the exe...even after opendialog has executed.

I'd like to do what this code looks like it could do (but can't)

current.directory := ExtractFilePath(Application.ExeName);
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 44

Expert Comment

by:CrazyOne
ID: 6462847
Perhaps this is what you are looking for.

ChDir(ExtractFilePath(Application.ExeName));
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 150 total points
ID: 6462851
Or perhaps this

SetCurrentDir(ExtractFilePath(Application.ExeName));

Delhi Help says

Sets the current directory.

Unit

Sysutils

Category

file management routines

function SetCurrentDir(const Dir: string): Boolean;

Description

The SetCurrentDir function sets the current directory. The return value is True if the current directory was successfully changed, or False if an error occurred.
0
 
LVL 5

Author Comment

by:Gwena
ID: 6462973
Thanks CrazyOne :-)
  That works perfectly! exactly what I was looking for :-)

gwen..
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6462979
You are welcome qwena :>)
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

Title # Comments Views Activity
Path  to current project in Delphi. 2 72
find a node in VST 2 64
Create a path if not exists 7 69
Delphi and Access based Enumeration 9 48
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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

26 Experts available now in Live!

Get 1:1 Help Now