C# Path GetTempFileName() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

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.

C#
string file = Path.GetTempFileName();

try {
    File.WriteAllText(file, "scratch data");
    Console.WriteLine(File.ReadAllText(file));
} finally {
    File.Delete(file);
}
CallWhat it does
Path.GetTempFileName()Creates an empty .tmp file and returns its path.
File.Delete(file)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

ParameterDescription
NoneGetTempFileName() takes no parameters.

Return Value

Return ValueDescription
stringThe 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));
    }
}

How It Works

  1. A real file appears. Right after the call, File.Exists is True. No other Path method does that, except for this one.
  2. It is empty. FileInfo.Length is 0, so the file contains no bytes yet.
  3. 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.
  4. 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));
    }
}

How It Works

  1. The file is ready to use. It already exists, so WriteAllText simply replaces its empty contents.
  2. Read it back. ReadAllText returns hello. The file now holds five bytes, so Length is 5 (one byte per character for plain English text).
  3. 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.
  4. 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)));
    }
}

How It Works

  1. 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.
  2. Two real files. Both files were created by the calls, so File.Exists is True for each.
  3. 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.
  4. 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.

Next: GetTempPath()

Get the path of the temp folder of the current user or system, with a trailing separator.

GetTempPath() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful