Re: How to get C:\Windows\Temp directory path in a computer?
On Sun, 13 Jul 2008 19:15:00 -0700, Landon
<Landon@discussions.microsoft.com> wrote:
I use VC++ MFC 4.2.
I need to get the C:\Windows\Temp on each computer on which my application
run. I will save a Temporary data there.
You need to save temporary files to the proper temp folder, which hasn't
been C:\Windows\temp in ages.
I have tried this:
LPITEMIDLIST lpItemIDList = NULL;
CString sDir;
if( SUCCEEDED( SHGetSpecialFolderLocation( NULL, CSIDL_WINDOWS,
&lpItemIDList ) ) ) {
char cWinPath[MAX_PATH + 1] = { NULL };
The NULL macro notionally represents a null pointer, but above, you're
using it as char(0), which works only because NULL is some integer zero
value. You shouldn't use NULL in this way, and here, you can just drop the
entire initializer, because SHGetPathFromIDList will overwrite it anyway.
SHGetPathFromIDList( lpItemIDList, cWinPath );
sDir.Format( "%s%s", cWinPath, "\\Temp" );
You forgot to CoTaskMemFree the LPITEMIDLIST.
}
return sDir;
}
but it was error, I guess the VC 4.2 is not support CSIDL_WINDOWS because I
read on MSDN Library it was mentioned "Version 5.0" for CSIDL_WINDOWS.
The "Version 5.0" refers to the shell32.dll version, not the VC version.
Is there any other way to get that path I need?
I am thinking about the registry key but I don't know which key to be read
to get the C:\Windows\Temp. I only know:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders
or
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders
No, don't read the registry to obtain any path. Modern versions of Windows
even contain a string value telling you not to:
: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
: Name: !Do not use this registry key
: Value: Use the SHGetFolderPath or SHGetKnownFolderPath function instead
That said, to deal with the temp folder, you should use GetTempPath and
GetTempFileName instead of the ShXXX API.
--
Doug Harrison
Visual C++ MVP