Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifically Getting the System TEMP Path in C#

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.

like image 410
Brian Avatar asked Mar 20 '15 18:03

Brian


People also ask

How do you find temp path?

The GetTempPath function returns the properly formatted string that specifies the fully qualified path based on the environment variable search order as previously specified. The application should verify the existence of the path and adequate access rights to the path prior to any use for file I/O operations.

What is path GetTempPath ()?

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.

How do I make AC temp folder?

Open your File Explorer (it's usually the first button on your desktop taskbar, looks like a folder). Go to the "This PC" section on the left, and then double-click your C: drive. On the Home tab at the top, click "New Folder" and name it "Temp".


1 Answers

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

like image 179
sudheeshix Avatar answered Oct 23 '22 04:10

sudheeshix