Specifically Getting the System TEMP Path in C#

Brian picture Brian · Mar 20, 2015 · Viewed 13.2k times · Source

I am using the System.IO.Path.GetTempPath() method to retrieve the temporary folder from environment variables. However, I am finding that this will always return the TEMP or TMP variable for the current User if it exists otherwise it will return the System TEMP or TMP variable.

Is there a way to always get the System TEMP variable?

I am aware of several other questions on SO about the Path.GetTempPath() method where answers are referencing the documentation from MSDN about how this method decides what to return. I am aware of the behavior of this method from MSDN and I am asking if there is another way to ensure I am getting the System Temporary Folder.

Answer

sudheeshix picture sudheeshix · Mar 20, 2015

Perhaps you are looking for the Environment.GetEnvironmentVariable method.

This usage gives you the user's %TEMP% folder:

Environment.GetEnvironmentVariable("TEMP");

such as C:\Users\MyUserName\AppData\Local\Temp

And this gives you the system's %TEMP% folder:

Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine);

such as C:\WINDOWS\TEMP