Return to Snippet

Revision: 11784
at February 18, 2009 13:08 by jimfred


Updated Code
// Using MFC. 
CString cwd;
DWORD cwdLen = ::GetCurrentDirectory( MAX_PATH, cwd.GetBufferSetLength(MAX_PATH+1) );
cwd.ReleaseBuffer( cwdLen ); // Note that ReleaseBuffer doesn't need a +1 for the null byte.

// Alternate approach, in C, using Win API.
static LPCSTR GetDefaultDirectory()
{
   static TCHAR szDirectory[MAX_PATH] = {0}; // place to remember path found.

   if ( !szDirectory[0] ) // if already found.
   {
      ::GetCurrentDirectory(sizeof(szDirectory) - 1, szDirectory); // in winbase.h
   }

   return szDirectory;

}

Revision: 11783
at February 18, 2009 13:05 by jimfred


Updated Code
// Using MFC. 
CString cwd;
DWORD cwdLen = ::GetCurrentDirectory( MAX_PATH, cwd.GetBufferSetLength(MAX_PATH+1) );
cwd.ReleaseBuffer( cwdLen + 1 );

// Alternate approach, in C, using Win API.
static LPCSTR GetDefaultDirectory()
{
   static TCHAR szDirectory[MAX_PATH] = {0}; // place to remember path found.

   if ( !szDirectory[0] ) // if already found.
   {
      ::GetCurrentDirectory(sizeof(szDirectory) - 1, szDirectory); // in winbase.h
   }

   return szDirectory;

}

Revision: 11782
at February 18, 2009 11:11 by jimfred


Initial Code
static LPCSTR GetDefaultDirectory()
{
   static TCHAR szDirectory[MAX_PATH] = {0}; // place to remember path found.

   if ( !szDirectory[0] ) // if already found.
   {
      ::GetCurrentDirectory(sizeof(szDirectory) - 1, szDirectory); // in winbase.h
   }

   return szDirectory;

}

Initial URL
http://www.pages.drexel.edu/~mfp27/cppfaqs/

Initial Description
Sometimes I want a complete path to a file in the current working directory to make error messages clearer. The URL has alternate approaches.

Initial Title
Get current working directory

Initial Tags
c, windows

Initial Language
C