How to get temporary folder for current user
Asked Answered
S

3

452

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\

Sidonia answered 3/6, 2009 at 12:35 Comment(5)
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\".Sidonia
Perhaps the 'administrator' account has a temp folder of C:\Windows\Temp indeed?Watchful
Is there any specific reason why you want to get the temporary path under C:\Documents and Settings\ always?Chloropicrin
FYI: if you do want the system temp folder, not the user's (if set) you can use Environment.GetEnvironmentVariable("temp", EnvironmentVariableTarget.Machine)Cockerel
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?Conchiferous
S
469

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.

Spandrel answered 3/6, 2009 at 17:26 Comment(4)
The TEMP environment variable would be set in two places: for the user itself, and for the local machine. If it's not set for the user, then the one for the local machine would be used, and that's always set in a default installation. Therefore the search usually stops on the second step. In testing, if TMP, TEMP and USERPROFILE are all unset, it actually does fall back to %SystemRoot% (C:\Windows\).Berm
Code that is executed in Windows Services that run under "Local System" or in applications that were started by such a service, the C:\Windows\Temp folder is sometimes used as termporary folder.Argentina
The documentation from your link says this: "The returned string ends with a backslash, for example, "C:\TEMP". But their example doesn't actually end with a backslash.Foredoom
You are not guaranteed that you will have writing permission to the path returned by that function in your application. What about if you tried IsolatedStorage instead?Durst
W
58

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.

Watchful answered 3/6, 2009 at 12:45 Comment(7)
@ImmortalBlue Because that's not necessarily the temp directory. The correct way to get the temp directory is to call System.IO.Path.GetTempPath().Mide
I know this is pretty old but I figured I'd leave a note for anyone curious: Not only is it not necessarily the temp directory, as @DavidHeffernan said, but you simply have no clue what it is. While not as much of an issue with .NET, it's considered poor practice because any malicious user could have altered that environment variable in order to exploit your program. If you're running with admin privileges and the malware can alter your code as it's executing, they can, for example, execute shellcode as an admin because it's been stored in TEMP.Marinna
There are 2 main differences between GetEnvironmentVariable("TEMP") and GetTempPath(). GetTempPath() checks first for "TMP" variable then "TEMP" and finally "USERPROFILE". If don't find any of those its returns path to Windows folder. It also guarantee that it returns correct path but it's not guarantee that the path exists.Compost
But this is exactly what the accepted answer does if you don't have a TMP variable (default scenario on recent Windows versions), isn't it?Liles
GetTempPath relies on your environment variables anyway, if you read the kernel documentation, so as user Logman points out there is no reason not to use GetEnvironmentVariable ... they both have the same weakness.Sickroom
If people override the TEMP environment variable, they do it for a reason. It's overridable for a reason. Applications should respect that if it's overriden.Necroscopy
Oh I see. GetTempPath returns the overriden folder as well if TEMP is overriden. So it doesn't matter either way. (I have overriden TEMP and TMP to point to my Dev Drive, as recommended in learn.microsoft.com/en-us/windows/dev-drive It looks like all apps respect that).Necroscopy
J
27

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.

Jackson answered 21/6, 2012 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.