Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\

like image 872
Anoop Avatar asked Jun 03 '09 12:06

Anoop


People also ask

How do I get to my temp folder?

The simplest way to access the user temp folder is through Windows search: Open Windows Search by clicking on it or with the Win + S shortcut, then enter “%temp%” into the search box, and then click on its entry when it comes up.

Where can users save temporary files?

The Temporary files created by the Windows operating system are usually stored in the %system%\Windows\Temp folder, whereas the ones created by the User when running any software is stored in his user profile at %userprofiles%\AppData\Local\.

Where is the temp folder in Windows?

How do I find that folder and open it? The simplest way is to let Windows find and open it for you, as follows: Windows 10: In the box in your taskbar that reads Search the Web and Windows, type %TEMP% including the percent signs, and press Enter. Your temp folder will open.


2 Answers

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.

like image 101
Niall Connaughton Avatar answered Oct 08 '22 13:10

Niall Connaughton


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.

like image 37
Helen Avatar answered Oct 08 '22 13:10

Helen