Page 1 of 1

Finding the directory where the .exe file is

Posted: Fri Jun 24, 2011 9:23 pm
by oftit
Hello all

I've been trying to find out how to find the directory to the .exe file that is executing. Because when I use GetCurrentDirectory() I get the directory where the program is loaded from. Like when I right click on "textFile.txt" and choose "Open with..." and then select the .exe program, the current directory for the program will now be the directory for the "textFile.txt" and not the directory for the .exe program is in.

Thank you in advance.

Re: Finding the directory where the .exe file is

Posted: Fri Jun 24, 2011 9:28 pm
by shadow
On Windows you are able to do it with the API:
GetModuleFileName_(#NULL)
or with standard PureBasic

Code: Select all

GetPathPart(ProgramFileName())

Re: Finding the directory where the .exe file is

Posted: Sat Jun 25, 2011 5:22 am
by MachineCode
GetCurrentDirectory() doesn't relate to your exe's location; it relates to the directory that your exe is USING.
Here's an example which should make it clear. Make the following code a standalone executable, and run it.

Code: Select all

exedir$=GetPathPart(ProgramFilename())

curdir$=GetCurrentDirectory()
MessageRequester("Test 1/2","Exe location:  "+exedir$+#CR$+"Current Dir  :  "+curdir$)

SetCurrentDirectory("C:\Program Files\")
curdir$=GetCurrentDirectory()
MessageRequester("Test 2/2","Exe location:  "+exedir$+#CR$+"Current Dir  :  "+curdir$)

Re: Finding the directory where the .exe file is

Posted: Sat Jun 25, 2011 10:55 am
by oftit
Alright when I use ProgramFilename() save the .exe file in some directory, and right click on a random file, and say "Open With...", and I choose my program that I just saved, the ProgramFilename() will now be the directory where the random file is placed????

Re: Finding the directory where the .exe file is

Posted: Sat Jun 25, 2011 11:43 am
by oftit
My bad it works. Thank you very much