Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to change current dir

Posted on 2001-09-06
8
Medium Priority
?
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 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