Coder Perfect

How do I get the current user’s temporary folder?

Problem

To get the temporary folder path for the current user, I’m currently using the following function:

string tempPath = System.IO.Path.GetTempPath();

On some PCs, it gives me the current user’s transient folder path, such as:

On other PCs, it gives me the path of the system temp folder, which looks like this:

According to the MSDN documentation, the aforementioned API returns the current system’s temporary folder.

Is there another API that can provide me the path to the current user’s temporary folder, such as this:

Asked by Anoop

Solution #1

System.IO.Path. GetTempPath() is simply a wrapper for Kernel32’s GetTempPath(..) function.

Visit http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx for more information.

This is what I copied from that page:

I’m not sure if “The Windows directory” refers to the transient directory under Windows or the actual Windows directory. Dumping transient files into the Windows directory itself appears to be a bad idea, but who knows.

So, based on that website and your message, I’m guessing that one of your Administrator user’s TMP, TEMP, or USERPROFILE variables points to the Windows path, or that they’re not set and it’s falling back to the Windows temp path.

Answered by Niall Connaughton

Solution #2

This should not be used:

System.Environment.GetEnvironmentVariable("TEMP")

The TEMP variable is not always the directory because environment variables might be modified.

System.IO.Path is the proper method. As in the accepted answer, GetTempPath().

Answered by Helen

Solution #3

We want to put logs in a specified root directory that should exist within the environment, and I have the same requirement.

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

I should be able to utilize Path to combine this with a sub-directory. Mix and match ( … ).

The GetFolderPath function provides a specific folder options overload that lets you choose whether the specified path is constructed or just confirmed.

Answered by IAbstract

Solution #4

try

Environment.GetEnvironmentVariable("temp");

Answered by Ikke

Post is based on https://stackoverflow.com/questions/944483/how-to-get-temporary-folder-for-current-user