MFC: GetCurrentDirectory function
21,340
Solution 1
GetCurrentDirectory is a simple Win32 API function, so just call it like this:
TCHAR currentDir[MAX_PATH];
GetCurrentDirectory( MAX_PATH, currentDir );
Solution 2
I assume you are trying to get the directory where your .exe file is located instead of the current directory. This directory can be different from the current directory.
TCHAR buff[MAX_PATH];
memset(buff, 0, MAX_PATH);
::GetModuleFileName(NULL,buff,sizeof(buff));
CString strFolder = buff;
strFolder = strFolder.Left(strFolder.ReverseFind(_T('\\'))+1);
Author by
stanigator
Updated on July 21, 2022Comments
-
stanigator almost 2 years
I know that GetCurrentDirectory() and SetCurrentDirectory() functions exist on the MFC framework, but I don't have a CFtpConnection object in my application. I have a simple CWinApp-derived class, and I would like to retrieve its working directory upon program startup. What's the easiest method to achieve this goal? Thanks in advance for the advices.
-
Mar almost 10 yearsThere is the same code, but using CString class (don't forget to call
ReleaseBuffer()
for yourCString
object):CString curDir; GetCurrentDirectory( MAX_PATH, curDir.GetBufferSetLength(MAX_PATH)); curDir.ReleaseBuffer();
-
Alex over 9 yearsThe path of the exe (or dll) is not necessarily the current directory. As soon as this is not the case any more you will run into problems if you use this solution.
-
www.diwatu.com over 9 yearsWhat you are talking about? this solution is just to avoid the case which current directory is different from the directory the exe file is. The question changed from he original asked.