4

The question How to get temporary folder for current user nicely describes how to go about finding the temp folder to be used for the current user.

In the documentation of Path.GetTempPath it notes only one exception which can arise due to security permissions.

There is no mention of failure modes if no temp folder can be located.

Is the no-temp-folder case realistic? Or would a missing temp folder indicate something like Windows being more generally trashed?

I guess this question boils down to: does an application that needs the temp folder attempt need to detect / recover / gracefully handle if there isn't one? Or is that so outside what should ever reasonably occur on a working Windows PC that you can just blindly rely on it?

(Personally I have never run into a PC without a working temp location, but we have occasional reports from end users where this seems to be the case. I could imagine some noise in that information, however).

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • 1
    I would label this as #meteorsFromSpace probability. Is is possible a meteor from space would come down and land on my car? Possible, yes. It is likely? No. So I'm not going to add (extra$$$) coverage to my car insurance to cover meteors from space hitting my car? Not really. – granadaCoder Aug 27 '19 at 13:22
  • Many processes in windows use the temp folder. I think the windows system itself will fail without the temp folder – JessGabriel Aug 27 '19 at 13:24
  • @JessGabriel that is what I was thinking also, I was hoping for some kind of documentation or Windows specifications that would make this clear. – StayOnTarget Aug 27 '19 at 14:08

3 Answers3

3

We have a missing temp folder error on a specific Windows Server system - there must be some misconfiguration or similar. Also, the user has the rights to manually create the folder, so we just added a "create temp folder if missing" safeguard during startup.

But yeah, it certainly is very unexpected behaviour and I have seen it only on maybe 1 in 1000+ systems.

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the path specified
   at Microsoft.Win32.NativeMethods.CreateDirectory(String path, SafeLocalMemHandle acl)
   at System.CodeDom.Compiler.TempFileCollection.CreateTempDirectoryWithAce(String directory, String identity)
   at System.CodeDom.Compiler.TempFileCollection.GetTempFileName(String tempDir)
   at System.CodeDom.Compiler.TempFileCollection.EnsureTempNameCreated()
   at System.CodeDom.Compiler.TempFileCollection.AddExtension(String fileExtension, Boolean keepFile)
   at System.CodeDom.Compiler.TempFileCollection.AddExtension(String fileExtension)
   at Microsoft.CSharp.CSharpCodeGenerator.FromSourceBatch(CompilerParameters options, String[] sources)

Yet to try this, but seems like it's very little overhead and save the sporadic hassle, in C#:

    private void GenerateTempPathIfNeeded()
    {
        var path = Path.GetTempPath();
        try
        {
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        }
        catch (Exception ex)
        {
            MessageBox.Show($"something");
        }
    }
Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104
  • So GetTempPath() itself doesn't throw if the folder doesn't exist... that's useful info – StayOnTarget Dec 08 '20 at 11:30
  • It's just a string, same as %path%. Also, we never used that folder directly (as for that exception) but rather for dynamic compilation, which seems to internally rely on a temp folder. I really assume the admin messed something up on that (customer) system in our case. – Andreas Reiff Dec 08 '20 at 18:22
  • @AndreasReiff I'm seeing this exact stack trace in a custom internal application, when compiling an Excel assembly for exporting data. It only happens for a single user, with the application being run interactively on a shared server. Do you know if this would be looking for a *Windows* Temp folder, or one that's user-specific? – Matt Sach Nov 15 '22 at 12:22
  • @MattSach It's a long time ago, typically it is %TEMP%, which is user specific. Strangely, I even had this change on the same user, having a subfolder like 1, 2, 3. – Andreas Reiff Nov 15 '22 at 15:14
  • @AndreasReiff Thanks for the reply. Sounds familiar: just had one user turn up a subfolder "48" !! Still no closer to why just one user is hitting this problem, given their values for temp folder env vars are present and they can manually create files there. Very odd. – Matt Sach Nov 15 '22 at 15:42
1

Use GetTempPath to retrieve this folder.

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

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

Microsoft says

  • The Windows directory is a strange choice of default for this function, since standard users can't create files there. The system default should be `"%ProgramData%\Temp"`, which should be set up to to allow standard users to create files and subdirectories that are accessible only to the creator, administrators, and SYSTEM. – Eryk Sun Aug 28 '19 at 14:37
0

It's a subjective question, but I would treat its unavailability as a fatal error, the same as I would do with a TEMP folder that is inaccessible due to security permissions.

I've seen the security error when an ASP.NET application failed to generate temporary files (e.g. XML Serialization assembly) in the following circumstances:

  • running under an account without a profile (so no account-specific TEMP directory)
  • did not have permission to access the %Windows%\Temp folder
Joe
  • 122,218
  • 32
  • 205
  • 338
  • In which case you should treat it as a fatal error. You (or a malicious hacker who got access to your machine) could provoke the error by running `SET TEMP=nonexistent-directory` prior to running your app, and it's probably best to just fail. – Joe Aug 27 '19 at 13:33