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.
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
Parameter
Description
None
GetTempPath() takes no parameters.
Return Value
Return Value
Description
string
The 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));
}
}
Output
Not empty: True
Rooted: True
Exists: True
Ends with separator: True
How It Works
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.
A full path.IsPathRooted is True: the result starts at a root, such as a drive or /, and not at the working folder.
A real folder.Directory.Exists is True, which is why you can write files there straight away.
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));
}
}
Output
Plus equals Combine: True
Trimmed plus equals Combine: False
Combine on trimmed equals Combine: True
app.log
How It Works
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.
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.
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.
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.
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.
Only text at first. Before CreateDirectory, the folder does not exist, so the first line is False. Building a path never creates anything.
Create and fill. After CreateDirectory and WriteAllText, the folder exists and holds exactly one file, so Directory.GetFiles(folder).Length is 1.
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.