Solved

How to change current dir

Posted on 2001-09-06
8
485 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

22 Experts available now in Live!

Get 1:1 Help Now