# Working Directory Changes

This topic is 2241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wrote a very small program to check the working directory of itself. It works fine when I just run the exe. However, when I drag a file on the exe to run it, the program thinks the working directory is C:\Documents and Settings\myusername

Does anyone know why this is and how I can have it correctly detect the working directory even when I drag a file onto the exe?

Here's the code I'm using:

 #include "direct.h" #include "stdio.h" #include "conio.h" void main() { char* buffer; buffer=_getcwd(NULL,0); printf("working directory: %s \n\n",buffer); getch(); } 

And here's the code, exe & 2008 Express project files:
http://www.mediafire...b73k885hxs8sx9a

Any help is appreciated. Thanks!

##### Share on other sites
It probably is correctly detecting the working directory. The working directory can and will change depending on how you start a program.

##### Share on other sites
Do you want the working directory, or the directory in which the executable resides?

##### Share on other sites
Sorry, I forgot to mention one critical detail: it doesn't matter where the file being dragged onto the exe comes from! Even if it's from the same directory as the exe, it will always say C:\Documents...

And yes, I need the directory where the exe is as I need assets loaded from subdirectories. Edited by GameCreator

##### Share on other sites
On Windows you can use GetModuleFileName() to get the fully qualified name of the executable and then manually strip off the file name portion of the string to get the directory.

##### Share on other sites
Thank you. Now I'm trying to figure out why the following only returns a single letter:

TCHAR buffer[MAX_PATH]; GetModuleFileName(NULL,buffer,MAX_PATH); printf("working directory: %s \n\n",buffer);

##### Share on other sites
Probably because you're compiling with UNICODE enabled, but trying to use narrow character functions to output the data. Some options include changing your project properties from using Unicode to Multi-byte, explicitly using GetModuleFileNameA() or switching to a wide-character output function.

##### Share on other sites
One thing is probably worth clarifying: the working directory for a process isn't necessarily the directory in which the executable resides. It is merely a directory that is logically prepended to relative paths in order to form an absolute path for the filesystem.

A process can change it's own working directory as it runs, and it's up to whatever initiates a process to set its initial working directory. When you run a program from the Windows command shell, that program's working directory will typically inherit the command shell's working directory (often displayed as part of the prompt). Windows Explorer and your IDE also set the working directory of the processes you ask them to spawn, but their rules for what it should be differ from the command prompt. Edited by edd²

##### Share on other sites
Thank you both again. SiCrane, your solutions worked (the two I tried). I really appreciate it!!

##### Share on other sites
EDIT: sorry, accidentally quoted myself rather than editing typo in original post Edited by edd²

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631354
• Total Posts
2999491
×