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.
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.
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.
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".
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With