Everybody developing with .NET knows the method Path.GetTempFileMethod. This method creates a zero-byte file in users temp directory and returns the full path of it. Sample code (seen in similar ways in many production environments) may look like this:
using System.IO;
public class Foo
{
public void Bar()
{
var tempFileName = Path.GetTempFileName();
using (var fileStream = File.Open(tempFileName, FileMode.Open))
{
// do something
}
}
}
In this little sample, there are some points of failure, that might not be clearly at all
(by the way: if you read this and you find some other potential sources of errors that should be discussed, tell me by commenting this article):
# Path.GetTempFileName throws an IOException if no temporary file can be created. This might be the (improbable) case if the currently used account has no rights to create files in its temporary folder. But a common case for that is, that no file can be created because the range of temp file names is gone. Path.GetTempFileName internally uses GetTempFileName Function of kernel32.dll. According to the functions unique file name creation algorithm, it is not suggested for creating large numbers of temporary files (as also said in its MSDN documentation). In the way the kernel function is used by the .NET method, only 65535 unique file names can be created. In production systems this might be not enough. So keep this in mind, when you use Path.GetTempFileName.
# Path.GetTempFileName not only creates a unique file name, as the name of the method suggests. It also creates a physical file. This is “only” required to reserve the unique file name. After that, something is done with that file, e.g. opened. As you can see, hard disc gets accessed two times. First for file name creation, second for working with that file. In my opinion, the first access should be avoided… (I know, this is a very little detail. But a penny saved is a penny got…
)
# Deleting temporary files after working with them should be obligatory. Real world tells us, that this is hardly done (look at the waste in your temp directory…). Also in the upper sample deleting doesn’t take place. So it will run into an exception by the 65536th run at the latest, because there are only 65536 available temp file names.
To solve the mentioned issues, I wrote a little class:
using System;
using System.IO;
namespace FileStreaming
{
public class FileStreamWithAutoDeletingFile
: FileStream
{
private readonly string _FilePath;
private FileStreamWithAutoDeletingFile(string filePath, FileMode mode, FileAccess access)
: base(filePath, mode, access)
{
_FilePath = filePath;
}
public static FileStream CreateNewTempFileForReadWrite()
{
var tempFileName = CreateTempFileName();
return new FileStreamWithAutoDeletingFile(
tempFileName,
FileMode.Create,
FileAccess.ReadWrite);
}
private static string CreateTempFileName()
{
/* in legacy implementation: Path.GetTempFileName()
*/
var tempDirectory = Path.GetTempPath();
var fileName = Guid.NewGuid().ToString() + ".tmp";
return Path.Combine(tempDirectory, fileName);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
try
{
if (!File.Exists(_FilePath))
{
return;
}
File.Delete(_FilePath);
}
catch
{ }
}
}
}
It may be used like this:
public void Bar2()
{
using (var fileStream = FileStreamWithAutoDeletingFile.CreateNewTempFileForReadWrite())
{
// do something
}
}
FileStreamWithAutoDeletingFile has a public method called CreateNewTempFileForReadWrite. This method creates a new file with a GUID based uniqe name and returns a stream for read/write access. When the stream gets disposed, the temporary file automatically gets deleted. This little helper solves all the upper issues: no more naming conflicts, no unnecessary hard disc access and no waste on the hard disc. All encapsulated nice and easily. 
Happy New Year!