In C#, Path.GetTempFileName()creates a real, empty file with a unique name in the temp folder and returns its full path. The file has 0 bytes and the extension .tmp, so the name can never clash with another call.
This is the one Path method that makes something on disk, and the file stays there until you delete it. Because the file already exists, you can write to it right away with File.WriteAllText and similar methods.
This is a static method of System.IO.Path. Call it as Path.GetTempFileName(...); there is no new Path(). Unlike most Path methods, this one creates a 0-byte file on disk in the temp folder. Delete it with File.Delete.
Syntax
The method takes no arguments. Always delete the file when you are done.
Removes it again. Required: nothing does it for you.
Path.GetRandomFileName()
Only a name, and no file is created.
Path.GetTempPath()
The folder that the file is created in.
Because the file already exists, a call that opens it with FileMode.CreateNew fails. Use FileMode.Create, OpenWrite, or File.WriteAllText, which are fine with an existing file.
Library declarations
C#
public static string GetTempFileName();
Parameters
Parameter
Description
None
GetTempFileName() takes no parameters.
Return Value
Return Value
Description
string
The full path of the new, empty temp file, such as one ending in .tmp. The name is unique in the temp folder.
C# Examples
Example 1 — GetTempFileName Creates an Empty File
Call it, inspect the file it made, then delete it. The name is never printed because it is random.
C#
using System;
using System.IO;
class Program {
static void Main() {
string file = Path.GetTempFileName();
Console.WriteLine("Exists: " + File.Exists(file));
Console.WriteLine("Size: " + new FileInfo(file).Length);
Console.WriteLine("Extension: " + Path.GetExtension(file));
Console.WriteLine("Rooted: " + Path.IsPathRooted(file));
File.Delete(file);
Console.WriteLine("Exists after delete: " + File.Exists(file));
}
}
A real file appears. Right after the call, File.Exists is True. No other Path method does that, except for this one.
It is empty.FileInfo.Length is 0, so the file contains no bytes yet.
A .tmp extension and a full path. The name ends in .tmp, and the path is rooted. The random part of the name is not printed, since it changes on every call.
Delete it yourself. After File.Delete, the check prints False. Without that line, the file would stay in the temp folder.
Example 2 — Use It as a Scratch File With try and finally
Write text into the file, read it back, and make sure the delete always runs.
C#
using System;
using System.IO;
class Program {
static void Main() {
string file = Path.GetTempFileName();
try {
File.WriteAllText(file, "hello");
string text = File.ReadAllText(file);
Console.WriteLine(text);
Console.WriteLine("Size: " + new FileInfo(file).Length);
} finally {
File.Delete(file);
}
Console.WriteLine("Cleaned up: " + !File.Exists(file));
}
}
Output
hello
Size: 5
Cleaned up: True
How It Works
The file is ready to use. It already exists, so WriteAllText simply replaces its empty contents.
Read it back.ReadAllText returns hello. The file now holds five bytes, so Length is 5 (one byte per character for plain English text).
finally always runs. Even if WriteAllText threw an exception, the finally block would still delete the file. This is the standard pattern for temp files.
Proof of cleanup. After the block, File.Exists is False, so !File.Exists(file) prints True.
Example 3 — Every Call Makes a New Unique File
Call the method twice, compare, and delete both files. Also check that both live in the temp folder.
C#
using System;
using System.IO;
class Program {
static void Main() {
string first = Path.GetTempFileName();
string second = Path.GetTempFileName();
Console.WriteLine("Different names: " + (first != second));
Console.WriteLine("Both exist: " + (File.Exists(first) && File.Exists(second)));
string tempFolder = Path.TrimEndingDirectorySeparator(Path.GetTempPath());
string folder = Path.GetDirectoryName(first);
Console.WriteLine("In the temp folder: " + string.Equals(folder, tempFolder, StringComparison.OrdinalIgnoreCase));
File.Delete(first);
File.Delete(second);
Console.WriteLine("Both deleted: " + !(File.Exists(first) || File.Exists(second)));
}
}
Output
Different names: True
Both exist: True
In the temp folder: True
Both deleted: True
How It Works
Unique every time. The system makes sure that a new name never matches a file that exists, so the two names differ and the first line is True.
Two real files. Both files were created by the calls, so File.Exists is True for each.
The same folder as GetTempPath.GetDirectoryName gives the folder part of the file path. After trimming the separator of the temp path and comparing without case, they match.
Delete both. Two calls created two files, so two deletes are needed. The last line confirms that neither one is left.
Frequently Asked Questions
It creates a unique, empty .tmp file in the temp folder and returns its full path. You can use that file as scratch space, then delete it.
Yes, with 0 bytes. This is different from GetRandomFileName and GetTempPath, which create nothing. Because it exists already, the name is reserved for you.
Yes. Nothing deletes it automatically, so put File.Delete in a finally block. Otherwise the file stays in the temp folder after your program exits.
GetTempFileName creates the file and returns a full path in the temp folder. GetRandomFileName only returns a random name such as abcdefgh.xyz and creates nothing, so you choose the folder.
The temp folder is full of .tmp files (about 65,000 on Windows), the disk is full, or the folder is not writable. Clean old files from the folder, or use your own random names in a private folder.