How I can get actual folder path where my program is without my exe file name in C++?
Asked
Active
Viewed 716 times
0
-
Dupe of http://stackoverflow.com/questions/737996/directory-of-running-program-on-linux/738024 - there are similar dupes for windows. – Jul 19 '09 at 12:51
-
Yep, for example: http://stackoverflow.com/questions/875249/how-to-get-current-directory/875264#875264 – Reunanen Jul 19 '09 at 12:57
-
Heh, seems to be your answer Neil :) – Reunanen Jul 19 '09 at 12:58
-
Yup, and on some systems (especially embedded) the question doesn't even make sense. C++ doesn't assume executables are files, nor does it assume a lot about the existance of (sub)directories. – MSalters Jul 20 '09 at 10:31
1 Answers
0
The following function will give you the application path:
::GetModuleFileName(NULL, szAppPath, MAX_PATH);
Now to extract the folder, you need to find the last backslash:
char szApplicationPath[MAX_PATH] = "";
::GetModuleFileName(NULL, szApplicationPath, MAX_PATH);
//Get the folder part
CString strApplicationFolder;
strApplicationFolder = szApplicationPath;
strApplicationFolder = strApplicationFolder.Left(strApplicationFolder.ReverseFind("\\"));

Francis B.
- 7,018
- 2
- 32
- 54