/*
The system keeps track of the process' current drive and directory, but it
does not keep track of the current directory for every drive. However, there
is some operating system support for handling current directories for
multiple drives. This support is offered via the process' environment
strings. For example, a process can have two environment variables, as shown
here:
=C:=C:\Utility\Bin
=D:=D:\Program Files
These variables indicate that the process' current directory for drive C is
\Utility\Bin and that its current directory for drive D is \Program Files.
If you call a function, passing a drive-qualified name indicating a drive
that is not the current drive, the system looks in the process' environment
block for the variable associated with the specified drive letter. If the
variable for the drive exists, the system uses the variable's value as the
current directory. If the variable does not exist, the system assumes that
the current directory for the specified drive is its root directory.
For example, if your process' current directory is C:\Utility\Bin and you
call CreateFile to open D:ReadMe.Txt, the system looks up the environment
variable =D:. Because the =D: variable exists, the system attempts to open
the ReadMe.Txt file from the D:\Program Files directory. If the =D: variable
did not exist, the system would attempt to open the ReadMe.Txt file from the
root directory of drive D. The Windows file functions never add or change a
drive-letter environment variable—they only read the variables.
NOTE
You can use the C run-time function _chdir or _wchdir instead of the Windows
SetCurrentDirectory function to change the current directory. The _chdir and
_wchdir function call SetCurrentDirectory internally, but _chdir also adds
or modifies the environment variables by calling SetEnvironmentVariable so
that the current directory of different drives is preserved.
*/
// There the sample of switching for different cases (current EXE file is
// located in the "D:\projects\richter-console" directory):
TCHAR szCurDir[MAX_PATH];
/* szCurDir has the C:\ */
DWORD cchLength = GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);
// change the value of the CURRENT DIRECTORY OF DISK C:
SetEnvironmentVariable(TEXT("=C:"), TEXT("C:\\Temp\\sandbox"));
// szCurDir has the C:\\Temp\\sandbox
cchLength = GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);
// Set the disk without the \\ suffix
SetCurrentDirectory(TEXT("D:"));
// szCurDir has the C:\Temp\sandbox
cchLength = GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);
// szCurDir has the D:\projects\richter-console
cchLength = GetFullPathName(TEXT("D:"), MAX_PATH, szCurDir, NULL);
// Set the disk without the \\ suffix
SetCurrentDirectory(TEXT("C:"));
// szCurDir has the C:\Temp\sandbox
cchLength = GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);
// Set the disk without the \\ suffix
_wchdir(TEXT("D:"));
/* szCurDir has the D:\ */
cchLength = GetFullPathName(TEXT("D:"), MAX_PATH, szCurDir, NULL);
// Set the disk with the \\ suffix
_wchdir(TEXT("D:\\"));
/* szCurDir has the D:\ */
cchLength = GetFullPathName(TEXT("D:"), MAX_PATH, szCurDir, NULL);
// change the value of the CURRENT DIRECTORY OF DISK D:
SetEnvironmentVariable(TEXT("=D:"), TEXT("D:\\SDK\\Autodesk"));
// Warning: switch to other disk before the next calling of the
// GetFullPathName function
SetCurrentDirectory(TEXT("C:"));
// szCurDir has the D:\SDK\Autodesk
cchLength = GetFullPathName(TEXT("D:"), MAX_PATH, szCurDir, NULL);
// Set the disk with the \\ suffix
SetCurrentDirectory(TEXT("C:\\"));
/* szCurDir has the C:\ */
cchLength = GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);