Temporary Directory thoughts

Hey all. Here's a random Q for you:

What do you do when looking for a temporary directory?

The Windows GetTempPath() function returns the following:
1. The path specified by the TMP environment variable.
2. The path specified by the TEMP environment variable, if TMP is not defined.
3. The current directory, if both TMP and TEMP are not defined.
4. An error.

Unfortunately for this to work, it requires that the user's environment have the following characteristic:
  a valid, writable directory must be specified by the given environment variable(s).
Otherwise you are just as bad off if the function returns the third or fourth outcome.


So, what is the likelihood that the user has boogered his system sufficiently that this will fail? (I don't trust the user.) And if he has, what do you recommend using instead?


I was considering the SP_CACHE (temporary internet files) directory, since that is automatically cleaned regularly... (unlike the TMP/TEMP directory, which Windows never seems to touch).

I was also considering just putting it under SP_LOCALAPPDATA (Local Application Data) as an appropriately named subdirectory...


Have any of you had to deal with this before?
What say ye?

(The question came to mind because of the possibility that a fatal error handler may wish to use a temporary file... so simply having the application fail is not a proper response when the user gets the "fatal error, want to send a report?" dialog.)
I deleted my last post, I have no idea how I missed that last paragraph but it pulls all of this together.

How long do you suppose you need to keep the data?

Is Event Viewer an option here?
Ah, yes. I am updating my Wallpapers application, and it needs to work on everything from Windows 9x to Windows 7, and I want there to be something in there just in case it crashes - to pop up a form that says

    An error I cannot fix has happened.
Would you like to email the author about it?

[    Yes, send email     ]    [   No, just quit   ]
[What's in the message?]    [         Help!         ]

I don't actually need to create a temporary file when this happens, but the thought crossed my mind and I began to wonder. (My first thought was to write the message to a temporary text file and use NotePad to display it, but that could fail too, so I'll just use a small window that simply displays the message text in a text box.)

How long do you suppose you need to keep the data?
Only as long as it takes for the user to finish looking at it, then it would be immediately deleted. But, just to keep options open, the timeframe is unspecified.

Is Event Viewer an option here?
No, for two reasons: The user may not have the application on his computer (assuming NotePad is on the user's PC is safer). And, you cannot trust the user.

Sorry I didn't make it clear that this was academic to begin with... but corners like these interest me.
Sorry, I'm really confused right now because you DID post this in the Windows section of the form. Event Viewer, as in
Start -> Control Panel -> Administrative Tools -> Event Viewer


This has been a component of the Windows OS for as long as I've been repairing PC's. I guarentee this is part of Windows 2K, XP, Vista and 7 I'm even willing to put 2:1 odds that it's part of Windows 98, 98SE and ME. This CANNOT BE UNINSTALLED by the user or any malware that I've seen and is the built in reporting system for exactly this kind of thing. Here's a link: http://support.microsoft.com/kb/815661
Sorry there don't appear to be any href tags in the page but straight C++ is toward the bottom.

As for the notepad thing, that's not a concern. When you save a file in Windows you as the programmer don't get to dictate what application gets to open it (how would you?). The OS looks at the extention of the file and checks a list of Registry entries to see what application the user wants to use, this is a user specific setting after all. So just by saving a file in plain text with a ".txt" extention you would ensure that as long as the end user has ANYTHING to open text files installed he would be able to read it.

EDIT: As an added bonus Event Viewer is periodically flushed. The buffer size has a default number of entries that can be modified by the user but if one of your goals is to minimize file clutter then this already does that for you.
Last edited on
Hmm... ya learn something new every day.
I'm not so sure that the Event Viewer is very useful for the end user though...

I understand the notepad issues (or lack thereof). I'm wondering about directories...

Thanks for all the info!
There is no guarantee that there will be a writable temp directory. The problem seems similar to the logfile problem (how can you log an error if there's an error with the logfile?)

You may have to restrict what you can do, and in the worse case give up.
Topic archived. No new replies allowed.