ChatGPT解决这个技术问题 Extra ChatGPT

How to get temporary folder for current user

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\

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\".
Perhaps the 'administrator' account has a temp folder of C:\Windows\Temp indeed?
Is there any specific reason why you want to get the temporary path under C:\Documents and Settings\ always?
FYI: if you do want the system temp folder, not the user's (if set) you can use Environment.GetEnvironmentVariable("temp", EnvironmentVariableTarget.Machine)
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?

C
Community

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.


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\).
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.
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.
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?
H
Helen

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.


@ImmortalBlue Because that's not necessarily the temp directory. The correct way to get the temp directory is to call System.IO.Path.GetTempPath().
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.
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.
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?
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.
I
IAbstract

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.