Path.GetTempFileName Method
Assembly: mscorlib (in mscorlib.dll)
This method creates a temporary file with a .TMP file extension.
The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.
The GetTempFileName method will raise an IOException if no unique temporary file name is available. To resolve this error, delete all unneeded temporary files.
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | |
| Write to a text file. | |
| Read from a text file. | |
| Retrieve a file extension. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve the file name and extension from a path. | |
| Retrieve only the file name from a path. | |
| Retrieve only the directory name from a path. | |
| Change the extension of a file. | |
| Determine if a directory exists. | |
| Determine if a file exists. |
- FileIOPermission for writing to the temporary directory. Associated enumeration: FileIOPermissionAccess.Write
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
Path ClassPath Members
System.IO Namespace
Other Resources
File and Stream I/OHow to: Read Text from a File
How to: Write Text to a File
Note: If you only need to open the temporary file once, then as mentioned by Catalin Pop Sever, it is better to pass the option FileOptions.DeleteOnClose when opening the file.
It is a common pattern to create a temporary file using Path.GetTempFileName, and wrap it in a try...finally block to ensure that it gets cleaned up:
C#:
string temporaryFile = null;
try
{
temporaryFile = Path.GetTempFileName();
// Use temporary file.
}
finally
{
if (temporaryFile != null)
{
try
{
File.Delete(temporaryFile);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
}
Writing the above code over and over can become both tiresome and error prone. So instead, extract it into own class:
using System;
using System.IO;
namespace System.IO
{
public class TemporaryFile : IDisposable
{
private bool _IsDisposed;
private bool _Keep;
private string _Path;
public TemporaryFile() : this(false)
{
}
public TemporaryFile(bool shortLived)
{
_Path = CreateTemporaryFile(shortLived);
}
public string Path
{
get { return _Path; }
}
public bool Keep
{
get { return _Keep; }
set { _Keep = value; }
}
~TemporaryFile()
{
Dispose(false);
}
public void Dispose()
{
Dispose(false);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_IsDisposed)
{
_IsDisposed = true;
if (!_Keep)
{
TryDelete();
}
}
}
private void TryDelete()
{
try
{
File.Delete(_Path);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
public static string CreateTemporaryFile(bool shortLived)
{
string temporaryFile = System.IO.Path.GetTempFileName();
if (shortLived)
{
// Set the temporary attribute, meaning the file will live in memory and will not be written to disk
File.SetAttributes(temporaryFile, File.GetAttributes(temporaryFile) | FileAttributes.Temporary);
}
return temporaryFile;
}
}
}
Using the new class is easy, just type the following:
using (TemporaryFile temporaryFile = new TemporaryFile())
{
// Use temporary file
}
If you decide, after constructing a TemporaryFile, that you want to prevent it from being deleted, simply set the TemporaryFile.Keep property to true:
using (TemporaryFile temporaryFile = new TemporaryFile())
{
temporaryFile.Keep = true;
}
- 6/12/2006
- David M. Kean
- 6/12/2006
- David M. Kean
A good programming pattern is to open temporary files with FileOptions.DeleteOnClose
// Note that File.Create() also overrides the file if it exists
using (FileStream fs = File.Create(Path.GetTempFileName(), Int16.MaxValue, FileOptions.DeleteOnClose)) {
// Use temp file
}
// The file will be deleted here
- 6/12/2006
- Catalin Pop Sever