C# Path GetTempPath() Method

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

Definition

In C#, Path.GetTempPath() returns the path of the temp folder: the place where programs may put short-lived files. On Windows it comes from environment variables such as TMP and TEMP. On Linux and macOS it is the TMPDIR variable, or /tmp/ when that is not set.

The result always ends with a separator, so you can add a name to it directly. The value is different on every machine, so tutorials and tests should print facts about it, not the path itself. The folder already exists, and the method does not create anything.

This is a static method of System.IO.Path. Call it as Path.GetTempPath(...); there is no new Path(). It only reads an environment variable and does not create files or folders. The returned folder normally exists already.

Syntax

The method takes no arguments and returns a path string.

C#
string temp = Path.GetTempPath();

string scratch = Path.Combine(temp, "my-app", "cache.bin");
Console.WriteLine(Directory.Exists(temp));   // True
CallWhat it does
Path.GetTempPath()The temp folder, ending with a separator.
Path.Combine(Path.GetTempPath(), "x")The usual way to build a path inside it.
Path.GetTempFileName()Creates a unique empty file inside that folder and returns its path.
Path.GetRandomFileName()A random name only, which you can add to the temp path yourself.

Other programs share the same folder. Choose names that are unlikely to clash (a random name is best) and delete what you create. On Linux and macOS the folder is shared by all users.

Library declarations
C#
public static string GetTempPath();

Parameters

ParameterDescription
NoneGetTempPath() takes no parameters.

Return Value

Return ValueDescription
stringThe full path of the temp folder, with a directory separator at the end. It is machine-specific.

C# Examples

Example 1 — Check Facts About the Temp Folder

The temp path is different on every machine, so print yes or no answers instead of the path.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string temp = Path.GetTempPath();

        Console.WriteLine("Not empty: " + (temp.Length > 0));
        Console.WriteLine("Rooted: " + Path.IsPathRooted(temp));
        Console.WriteLine("Exists: " + Directory.Exists(temp));

        char last = temp[temp.Length - 1];
        Console.WriteLine("Ends with separator: " + (last == Path.DirectorySeparatorChar));
    }
}

How It Works

  1. Not empty. The method always returns some text, so its length is above zero. The text itself is not printed, because every reader would see a different path.
  2. A full path. IsPathRooted is True: the result starts at a root, such as a drive or /, and not at the working folder.
  3. A real folder. Directory.Exists is True, which is why you can write files there straight away.
  4. The last character. It is the separator of the current system: a backslash on Windows and a slash on Linux and macOS. Comparing with DirectorySeparatorChar is therefore True on both.

Example 2 — Build a Scratch Path With Combine

Compare plain text joining with Combine. The first one works only because the temp path ends with a separator.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string temp = Path.GetTempPath();

        string a = temp + "app.log";
        string b = Path.Combine(temp, "app.log");
        Console.WriteLine("Plus equals Combine: " + (a == b));

        string trimmed = Path.TrimEndingDirectorySeparator(temp);
        string c = trimmed + "app.log";
        Console.WriteLine("Trimmed plus equals Combine: " + (c == b));

        string d = Path.Combine(trimmed, "app.log");
        Console.WriteLine("Combine on trimmed equals Combine: " + (d == b));
        Console.WriteLine(Path.GetFileName(b));
    }
}

How It Works

  1. The plus sign works here by luck. The temp path already ends with a separator, so temp + "app.log" is the same text as Combine would produce.
  2. Without the separator it breaks. TrimEndingDirectorySeparator removes the last one. Joining with + now glues the folder name and app.log together, so the comparison is False.
  3. Combine does not care. It adds a separator only if there is none, so the trimmed and the untrimmed paths lead to the same result.
  4. Print the safe part. GetFileName prints app.log alone, so the output does not show where the temp folder is on this machine.

Example 3 — Make a Private Work Folder and Clean Up

Use a random name inside the temp folder, create it, put a file in it, count the files, and delete everything.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string folder = Path.Combine(Path.GetTempPath(), "codetofun-" + Path.GetRandomFileName());

        Console.WriteLine("Before: " + Directory.Exists(folder));
        Directory.CreateDirectory(folder);
        File.WriteAllText(Path.Combine(folder, "a.txt"), "hello");

        Console.WriteLine("Created: " + Directory.Exists(folder));
        Console.WriteLine("Files: " + Directory.GetFiles(folder).Length);
        Console.WriteLine("Inside temp: " + folder.StartsWith(Path.GetTempPath(), StringComparison.Ordinal));

        Directory.Delete(folder, true);
        Console.WriteLine("Deleted: " + !Directory.Exists(folder));
    }
}

How It Works

  1. A name nobody else uses. GetRandomFileName gives eight random letters and digits, a dot, and three more. With the codetofun- prefix, the folder name will not clash with anything else in the temp folder.
  2. Only text at first. Before CreateDirectory, the folder does not exist, so the first line is False. Building a path never creates anything.
  3. Create and fill. After CreateDirectory and WriteAllText, the folder exists and holds exactly one file, so Directory.GetFiles(folder).Length is 1.
  4. Clean up. Directory.Delete(folder, true) removes the folder with its contents. It is safe here because the folder is one this program made inside the temp path. The last line proves that it is gone.

Frequently Asked Questions

The path of the temp folder with a trailing separator. On Windows it is usually inside the user's AppData\Local\Temp folder, and on Linux it is /tmp/ unless TMPDIR says otherwise.
No. It only reads the settings. The temp folder normally exists already, so you can use it right away. If it does not, create it with Directory.CreateDirectory.
So that adding a file name to it with plain text works. Path.Combine also handles it correctly, and it is the safer way to join anyway, because it does not rely on that detail.
Use a random name: Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()), then call Directory.CreateDirectory. Delete the folder with Directory.Delete(folder, true) when you are done.
Not reliably. Some systems clear it at restart or after some days, but your program should not count on it. Delete your own temp files when you finish with them.

Next: HasExtension()

Check whether the file name part of a path has an extension.

HasExtension() 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.

12 people found this page helpful