Reputation: 12998
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).
Upvotes: 4
Views: 2038
Reputation:
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.
Upvotes: 1
Reputation: 8404
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");
}
}
Upvotes: 3
Reputation: 124696
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:
%Windows%\Temp
folderUpvotes: 0