8

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.

stanigator
  • 10,768
  • 34
  • 94
  • 129
  • See http://msdn.microsoft.com/en-us/library/aa364934(VS.85).aspx ... nothing to do with whether or not you have a CFtpConnection object. – ChrisW Jun 17 '09 at 23:23
  • Dupe: http://stackoverflow.com/questions/875249/how-to-get-current-directory – ChrisW Jun 17 '09 at 23:40
  • Under normal circumstances the working folder is going to be found in the user application data folder and there is a special API to get special folder locations. So, you have to decide how you are managing where the working folder will be. – Andrew Truckle Jan 09 '17 at 03:55

2 Answers2

18

GetCurrentDirectory is a simple Win32 API function, so just call it like this:

TCHAR currentDir[MAX_PATH];
GetCurrentDirectory( MAX_PATH, currentDir );
Gerald
  • 23,011
  • 10
  • 73
  • 102
  • 2
    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();` – Mar Aug 12 '14 at 00:31
6

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);    
diwatu
  • 5,641
  • 5
  • 38
  • 61
  • 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. – Alex Nov 19 '14 at 14:14
  • 1
    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. – diwatu Jan 19 '15 at 08:07