4

I need to get a safe temp folder where I could store temporary files for my application, but so far my research has lead me to conclusion that all approaches I've found are flawed.

The first idea was to use GetTempPath function, but that causes two problems:

  • The folder might not exist, so I would have to truncate folders one by one up to root, and recreate them if they do not exist back to full path (error prone, tedious)
  • From "Larry Osterman's WebLog" click it seems that GetTempPath might fallback to USERPROFILE or Windows directory and extract whole lot of files right in there, which is SUPER BAD(TM)!

In the same post, there is a suggestion to use GetEnvironmentVariable, but this seems a dangerous function to me (missing TMP & TEMP envvars for instance).

Is there a cleaner function I could use? Seems that SHGetKnownFolderPath has no clue what temp folder is.

Coder
  • 3,695
  • 7
  • 27
  • 42
  • Directory.Create creates all directories on the way that do not exist and does nothing if the directory does exist. – ctusch Jul 10 '13 at 14:31

4 Answers4

3

Your program is probably not the only one to rely on GetTempPath, so it's reasonable to expect it to return a proper writable path. Especially since Windows automatically initializes the TMP and TEMP environment variables for you; someone would have to go to some trouble to override them, and it would be their responsibility to make sure the change did not mess up their system.

I would go ahead and assume GetTempPath works properly, and worry about failures when you try to create the temporary file - there are other errors that might occur at that time that you need to check for anyway.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • I decided to go with GetEnvVar as in article. This avoids problems with longer usernames and should be enough. If there is no TMP or TEMP variable, I think it's enough to assume that system is pretty much unusable. – Coder Apr 04 '11 at 20:43
  • -1 as the documentation specifically says _"Note that the function **does not verify that the path exists, nor does it test to see if the current process has any kind of access rights** to the path"_. – David Nov 18 '13 at 10:14
  • @David, of course it doesn't make that guarantee, that's not its job. Checking the folder would take lots of extra work that shouldn't be necessary on a properly configured system. I stand behind my answer. – Mark Ransom Nov 18 '13 at 12:13
0

An idea would be to get the path where your application is (GetModuleFileNameEx combined with GetModuleHandle(NULL) and GetCurrentProcess) since this directory cannot be deleted under windows as long as your application is running from it (maybe I'm wrong ...some years ago I couldn't do this :) ) and in this directory create a temporary directory.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

Your first bullet point is the solution. Wrap it up in a method so that you don't duplicate code.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

According to this answer, Boost's Filesystem library can be used for this.

Community
  • 1
  • 1
hlovdal
  • 26,565
  • 10
  • 94
  • 165