Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory changes

Status
Not open for further replies.

JurgenWellinger

Programmer
Aug 30, 2001
8
ZA
When using a File open or File Save dialog, how do I stop it from changing the working directory of the program? I.e. how do I stop it from affecting where other files are written on the hard drive?

Better yet, how do I determine where the .exe file is located (in which directory) from within the program so that I can save settings files in the same directory?

Thanks.
 
Call SetCurrentDirectory after the dialog is closed.

Otherwise you cannot prevent the user from changing it.

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
To find the location of an .exe file, try this:

// Get the file handle of the .exe you want
HMODULE hmodule = GetModuleHandle(_T("targetfile.exe"));

// Use the handle to find the full pathname + filename
// the result of GetModuleFileName() is stored in
// pathandfile. in this format:
// drive:\path1\path2\filename.exe

char pathandfile[MAX_PATH];
GetModuleFileName(hmodule,pathandfile,MAX_PATH);

Cheers !
 
This is how you get the current directory in case you need it:

#include <windows.h>

char szDirectory[MAX_PATH];
GetCurrentDirectory(lstrlen(szDirectory),szDirectory);

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Thanks, but to get it to work I had to change it as follows:

#include <windows.h>

char szDirectory[MAX_PATH];
GetCurrentDirectory(MAX_PATH,szDirectory);
 
When you open the save or open dialog the directory of the file selected is returned and set as the current directory for the app. You can change this with the following code. Over write Save so it will look like this:

void CMPCApp::OnFileSave()
{
char Buff[75];
GetCurrentDirectory(sizeof(Buff),Buff );

CMPCApp::OnFileSave();

SetCurrentDirectory(Buff);
}

By doing this you save the path before calling OnFileSave() and restore it after it's been called. This is being done in the CWinApp class but you can do it in your doc class, etc. Make sure you're buffer is large enough to hold the directory path your app will be in.


Brother C
 
As other suggested, you can use GetCurrentDirectory to save the current directory.

If you do not want the file dialog to change the directory, just add the flag in the &quot;Flags field:
OPENFILENAME ofn;
ofn.Flags = ... | OFN_NOCHANGEDIR;

HTH
Shyan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top