Question

I've written a program that calls the System.IO.Path.GetTempPath() function. All the documentation I've read (like this one) says that the function should return the first path found from this list:

  • 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.

I have defined both the TMP and TEMP environment variables to be %USERPROFILE%\AppData\Local\Temp, but the call to GetTempPath() always returns my %USERPROFILE% directory instead of the values I've define for TMP and TEMP. How can I get the function to return the temporary directory I've defined?

Was it helpful?

Solution

That is an Environment setting. http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx

var tmp = Environment.GetEnvironmentVariable("tmp");

UPDATE: I went to a command prompt and did

SET TMP=C:\Temp

Then I launched visual studio from my command prompt. Now my environment is updated and visual studio sees it. The code above (as well as yours) worked for me. It displayed the updated environment settings.

So I believe you would have to kill explorer or logoff in order to get the new environment to be seen permenantly.

OTHER TIPS

Did you create the TMP and TEMP variables after starting Visual Studio?

Try restarting VS, or run the application from Windows Explorer. Maybe even restart Windows.

Environment variables are assigned to a process when the process is created, and they won't change for that process. Also, child processes inherit the environment variables from their parent process. So running the application from a Visual Studio session that was started before the creation of the variables means the application will not have those variables available. So a restart is needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top