As the Windows O/S and the exploits of virus/ransomware and anti-virus software has evolved, so too has the generally-accepted location(s) where an application can safely write a temporary file (such as a PDF to be included as an email attachment) to disk.
I know I'm dating myself, but a L-O-N-GGGG time ago, the TEMP environment variable typically pointed to C:\Temp. Later, it was acceptable to write to the current user's "My Documents" (now just shortened to "Documents") folder, but this location is targeted by ransomware, so it's common now for only trusted apps to have access to this location. More recently, it looks like the TEMP environment variable now points to the C:\users\username\AppData\Local\Temp folder.
Question: The need for temporary files has not gone away, so where does your application write temporary files? I'd be very interested to learn what others are doing.
Many thanks in advance!
John
If for any reason that value is missing or the path is not valid, a cascading technique as illustrated in Mike's code snippet will be used, checking several paths in the following order: APPDATA, TEMP, TMP, PUBLIC, then USERPROFILE. Even though I don't need it in this particular case, Bruce's use of both the Local and Roaming subdirectories is very interesting and I appreciate him sharing it.