415
votes

Currently I am using following function to get the temporary folder path for current user:

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\

4
The behavior of System.Environment.GetEnvironmentVariable("TEMP") is same as GetTempPath(). In my machine for account 'administrator' both API returns "C:\WINDOWS\TEMP" but for account 'Network Service' both API returns "C:\Documents and Settings\Network Service\Local Settings\Temp\".Anoop
Perhaps the 'administrator' account has a temp folder of C:\Windows\Temp indeed?Helen
Is there any specific reason why you want to get the temporary path under C:\Documents and Settings\ always?Noldorin
FYI: if you do want the system temp folder, not the user's (if set) you can use Environment.GetEnvironmentVariable("temp", EnvironmentVariableTarget.Machine)piers7
In the past, I've used temp folders frequently and never cared where it was, as long as cleanup can happen so disc doesn't get totally consumed. Isn't the point of a temp folder for temporary use - junk placeholder? Why can't it be unknown and handled by the API? Should you use a configurable, well known location instead of temp?barrypicker

4 Answers

428
votes

System.IO.Path.GetTempPath() is just a wrapper for a native call to GetTempPath(..) in Kernel32.

Have a look at http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx

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.

42
votes

DO NOT use this:

System.Environment.GetEnvironmentVariable("TEMP")

Environment variables can be overridden, so the TEMP variable is not necessarily the directory.

The correct way is to use System.IO.Path.GetTempPath() as in the accepted answer.

24
votes

I have this same requirement - we want to put logs in a specific root directory that should exist within the environment.

public static readonly string DefaultLogFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

If I want to combine this with a sub-directory, I should be able to use Path.Combine( ... ).

The GetFolderPath method has an overload for special folder options which allows you to control whether the specified path be created or simply verified.

-8
votes

try

Environment.GetEnvironmentVariable("temp");