Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The directory at the end of the active path is called the current directory; it is the directory in which the active application started, unless it has been explicitly changed. An application can determine which directory is current by calling the GetCurrentDirectory function. It is sometimes necessary to use the GetFullPathName function to ensure the drive letter is included if the application requires it.
Note
Although each process can have only one current directory, if the application switches volumes by using the SetCurrentDirectory function, the system remembers the last current path for each volume (drive letter). This behavior will manifest itself only when specifying a drive letter without a fully qualified path when changing the current directory point of reference to a different volume. This applies to either Get or Set operations.
An application can change the current directory by calling the SetCurrentDirectory function.
The following example demonstrates the use of GetCurrentDirectory and SetCurrentDirectory.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define BUFSIZE MAX_PATH
void _tmain(int argc, TCHAR **argv)
{
TCHAR Buffer[BUFSIZE];
DWORD dwRet;
if(argc != 2)
{
_tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
return;
}
dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
if( dwRet == 0 )
{
printf("GetCurrentDirectory failed (%d)\n", GetLastError());
return;
}
if(dwRet > BUFSIZE)
{
printf("Buffer too small; need %d characters\n", dwRet);
return;
}
if( !SetCurrentDirectory(argv[1]))
{
printf("SetCurrentDirectory failed (%d)\n", GetLastError());
return;
}
_tprintf(TEXT("Set current directory to %s\n"), argv[1]);
if( !SetCurrentDirectory(Buffer) )
{
printf("SetCurrentDirectory failed (%d)\n", GetLastError());
return;
}
_tprintf(TEXT("Restored previous directory (%s)\n"), Buffer);
}
Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.
Documentation
GetCurrentDirectory function (winbase.h) - Win32 apps
Retrieves the current directory for the current process.
SetCurrentDirectory function (winbase.h) - Win32 apps
Changes the current directory for the current process.
GetFullPathNameA function (fileapi.h) - Win32 apps
Retrieves the full path and file name of the specified file. (ANSI)