3

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?

Community
  • 1
  • 1
Gillfish
  • 717
  • 1
  • 9
  • 29
  • Where did you define those environment variables? Are you sure they are actually defined for the program you are running? – Eric Petroelje Jul 17 '13 at 19:32
  • @EricPetroelje I defined those variables in the Environment Variables dialog in the Advanced System Settings control panel. If I type %TEMP% or %TMP% into the run dialog box (this is Windows 7) it will bring up the directory I would expect (`%USERPROFILE%\AppData\Local\Temp`) – Gillfish Jul 17 '13 at 19:38

2 Answers2

3

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.

phillip
  • 2,618
  • 19
  • 22
  • Yes, I set the temp path as an environment variable. My understanding is that setting that variable would affect the value returned by the `GetTempPath()` function, but it doesn't seem to. – Gillfish Jul 17 '13 at 19:42
  • 3
    Your answer prompted me to discover the problem, so I'm going to mark it as the answer. I have been launching Visual Studio for so long from a Cygwin command prompt (don't ask why, my company has setup our solution so that it only works that way) that I didn't even think about Cygwin redefining environment variables. Once I launched Visual Studio the normal way, from the Start menu, it returned the path I expected. – Gillfish Jul 17 '13 at 19:51
0

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.

  • I've restarted both Visual Studio and the computer since setting those variables. – Gillfish Jul 17 '13 at 19:35
  • Odd. How did you set them? Also, are they still set after the restart? –  Jul 17 '13 at 19:37
  • I defined them in the Environment Variable system dialog. See my comment above. And yes, they are still set after the restart. – Gillfish Jul 17 '13 at 19:45
  • the docs on msdn say that the function returns the current user's temp folder, so it may not be System wide. I haven't tested it, just looked it up on MSDN, – joebalt Jul 17 '13 at 19:47