Page 1 of 1

GetCurrentDirectory() - cross-platform

Posted: Sun Feb 26, 2006 9:52 pm
by Straker
Code updated For 5.20+ (same As GetCurrentDirectory())

I take no credit for this. This is ts-soft's code that I found in the Feature Requests forum, but too many people are still looking for it.

Code: Select all

Procedure.s GetCurrentDir()
  Protected CurDir.s
  CurDir.s = Space(2048)
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    getcwd_(CurDir, 2048)
  CompilerElse
    GetCurrentDirectory_(2048,CurDir)
  CompilerEndIf
  ProcedureReturn CurDir
EndProcedure

Debug GetCurrentDir()
The original thread is here.

Posted: Mon Feb 27, 2006 12:06 am
by Paul
One year earlier than above code...
viewtopic.php?t=13904&highlight=getcwd

Posted: Mon Feb 27, 2006 12:29 am
by PB
The FAQ says to use this:

Code: Select all

a$=Space(999) : GetModuleFileName_(0,@a$,999) : appdir$=GetPathPart(a$)
Why? Because you're definitely getting the correct path to the exe's folder.

I had problems using GetCurrentDirectory when booting up: my app was set
to run at startup and using GetCurrentDirectory was returning the wrong path.
As such, I've used the FAQ method ever since, with 100% success. FYI.

Also, don't forget that GetCurrentDirectory will CHANGE if your app calls the
SetCurrentDirectory command! Another reason to use the FAQ method instead.

Posted: Mon Feb 27, 2006 1:12 am
by Straker
@Paul. Thanks - but I was merely trying to make this entry in the Tips forum where it belongs.

@PB - You are right. This should not be confused with a "GetExeDirectory" or "GetApplicationPath" type function. This gets the currently set directory in the OS, even if your EXE is in a different directory. This is still a valid function. This posting was not intended to answer the FAQ you reference:

How do I know which folder my app was run from?

Posted: Mon Feb 27, 2006 1:21 am
by PB
> This posting was not intended to answer the FAQ

Sorry, my bad.

Posted: Mon Feb 27, 2006 1:24 am
by Dare2
hehe. I fell in the GetCurrentDirectory hole in front of *too many* users with a new install. :oops: So a heads-up is useful!

However, that is a good tip.

It could also be used for the Application folder (using the *nix version of GetModuleFileName, whatever that is) or for a lot of things, actually.

Thanks.