snippetcsharpCritical
How to get temporary folder for current user
Viewed 0 times
howtemporaryforcurrentuserfolderget
Problem
Currently I am using following function to get the temporary folder path for current user:
On some machines it gives me temp folder path of current user like:
C:\Documents and Settings\administrator\Local Settings\Temp\
On some machines it gives me system temp folder path like:
C:\Windows\TEMP
MSDN Documentation also says that above API returns current system's temporary folder.
Is there any other API available which gives me current user's temporary folder path like this:
C:\Documents and Settings\administrator\Local Settings\Temp\
string tempPath = System.IO.Path.GetTempPath();On some machines it gives me temp folder path of current user like:
C:\Documents and Settings\administrator\Local Settings\Temp\
On some machines it gives me system temp folder path like:
C:\Windows\TEMP
MSDN Documentation also says that above API returns current system's temporary folder.
Is there any other API available which gives me current user's temporary folder path like this:
C:\Documents and Settings\administrator\Local Settings\Temp\
Solution
System.IO.Path.GetTempPath() is just a wrapper for a native call to GetTempPath(..) in Kernel32.Have a look at https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha
Copied from that page:
The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable.
- The path specified by the USERPROFILE environment variable.
- The Windows directory.
It's not entirely clear to me whether "The Windows directory" means the temp directory under windows or the windows directory itself. Dumping temp files in the windows directory itself sounds like an undesirable case, but who knows.
So combining that page with your post I would guess that either one of the TMP, TEMP or USERPROFILE variables for your Administrator user points to the windows path, or else they're not set and it's taking a fallback to the windows temp path.
Context
Stack Overflow Q#944483, score: 483
Revisions (0)
No revisions yet.