Finding the directory where the .exe file is

Just starting out? Need help? Post your questions and find answers here.
oftit
User
User
Posts: 52
Joined: Sun Apr 25, 2010 5:08 am

Finding the directory where the .exe file is

Post 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.
User avatar
shadow
User
User
Posts: 34
Joined: Thu Dec 16, 2010 1:25 pm
Location: Germany

Re: Finding the directory where the .exe file is

Post by shadow »

On Windows you are able to do it with the API:
GetModuleFileName_(#NULL)
or with standard PureBasic

Code: Select all

GetPathPart(ProgramFileName())
ThinkPad T61 | PureBasic 4.51 | Windows 7 (32) | Kubuntu 11.10 (64) | Syllable (32)
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Finding the directory where the .exe file is

Post 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$)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
oftit
User
User
Posts: 52
Joined: Sun Apr 25, 2010 5:08 am

Re: Finding the directory where the .exe file is

Post 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????
oftit
User
User
Posts: 52
Joined: Sun Apr 25, 2010 5:08 am

Re: Finding the directory where the .exe file is

Post by oftit »

My bad it works. Thank you very much
Post Reply