MFC: GetCurrentDirectory function

mfc
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);    
Share:
21,340
stanigator
Author by

stanigator

Updated on July 21, 2022

Comments

  • stanigator
    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
    Mar almost 10 years
    There is the same code, but using CString class (don't forget to call ReleaseBuffer() for your CString object): CString curDir; GetCurrentDirectory( MAX_PATH, curDir.GetBufferSetLength(MAX_PATH)); curDir.ReleaseBuffer();
  • Alex
    Alex over 9 years
    The 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
    www.diwatu.com over 9 years
    What 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.