In C#, Path.Combine() joins two or more path pieces into one path string. It puts the separator of your system between the pieces, but only when one is missing: "data" and "file.txt" become data/file.txt on Linux and data\file.txt on Windows.
There is one rule to remember. If a later piece is rooted (it starts with a separator, or with a drive such as C:\), Combinethrows away everything before it. So Combine("a", "/b") is just /b. This helps for default paths, but is risky with user input.
This is a static method of System.IO.Path. Call it as Path.Combine(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.
Syntax
Pass two, three, four, or any number of pieces. The result is a new string; nothing is created on disk.
Joins three pieces. A four-piece form and a params string[] form take more.
Combine("data/", "file.txt")
No extra separator, because the first piece already ends with one.
Combine("data", "/etc/x")
The second piece is rooted, so the result is /etc/x and data is dropped.
Combine never adds a separator at the end: Combine("a", "b") is a/b, not a/b/. A piece that is an empty string is skipped. A null piece throws ArgumentNullException.
Library declarations
C#
public static string Combine(string path1, string path2);
public static string Combine(string path1, string path2, string path3);
public static string Combine(string path1, string path2, string path3, string path4);
public static string Combine(params string[] paths);
Parameters
Parameter
Description
path1, path2, ...
The pieces to join, from left to right. None of them may be null.
paths
The params string[] form: any number of pieces in one call or one array.
Return Value
Return Value
Description
string
The joined path. If a later piece is rooted, it starts from that piece. If every piece is empty, the result is an empty string.
C# Examples
Example 1 — Join Folders and a File Name
Join three pieces, see why Replace is used, and watch how an existing trailing slash is handled.
C#
using System;
using System.IO;
class Program {
static void Main() {
string path = Path.Combine("folder", "sub", "file.txt");
Console.WriteLine(path.Replace('\\', '/'));
string withSlash = Path.Combine("data/", "file.txt");
Console.WriteLine(withSlash);
Console.WriteLine("folder" + "file.txt");
Console.WriteLine(Path.Combine("folder", "file.txt").Replace('\\', '/'));
}
}
Why Replace? Windows would build folder\sub\file.txt and Linux builds folder/sub/file.txt. Replace('\\', '/') turns every backslash into a slash, so the printed text is the same on every system.
No double separator."data/" already ends with a separator. A forward slash counts as one on every system, so Combine adds nothing and the answer is data/file.txt.
The plus sign adds nothing."folder" + "file.txt" is plain text joining, so the pieces run together as folderfile.txt.
Combine fixes it. The same two pieces joined with Combine get a separator between them, which gives folder/file.txt.
Example 2 — A Rooted Piece Throws Away Earlier Pieces
Add a rooted piece in the middle of the list and see what is left. Then compare the safe way with Path.Join.
C#
using System;
using System.IO;
class Program {
static void Main() {
string a = Path.Combine("data", "/etc/config.json");
Console.WriteLine(a.Replace('\\', '/'));
string b = Path.Combine("logs", "2026", "/archive", "a.txt");
Console.WriteLine(b.Replace('\\', '/'));
string c = Path.Join("data", "/etc/config.json");
Console.WriteLine(c.Replace('\\', '/'));
Console.WriteLine(Path.IsPathRooted("/etc/config.json"));
}
}
The second piece is rooted."/etc/config.json" starts with a separator, so Combine forgets "data" and returns the rooted piece alone.
The last rooted piece counts. In the four-piece call, logs and 2026 are dropped when /archive arrives. Only /archive and a.txt remain.
Join keeps everything.Path.Join has no rooted rule. It sees that the second piece already starts with a separator, adds none, and glues the pieces to give data/etc/config.json.
A simple guard.Path.IsPathRooted is True for such a piece, so you can reject rooted names before you call Combine with input you do not trust.
Example 3 — Empty Pieces and null Pieces
Empty strings are skipped. A null piece is a mistake, and Combine says so with an exception.
An empty first piece. It adds nothing, and no separator is placed before file.txt, so the result is just file.txt.
An empty last piece. The result is folder, with no trailing separator. Combine never adds one at the end.
null is not the same as empty. The variable missing is null, so Combine throws ArgumentNullException. The catch prints the type name.
Replace null first.missing ?? "" changes null into an empty string. The call then works, and the empty middle piece is skipped like the ones in the first two lines.
Frequently Asked Questions
It joins path pieces into one string and adds the system separator between them when needed. It only builds text. It does not check that the folders or file exist.
Because a later argument is rooted. Combine("a", "/b") returns "/b": a path that starts with a separator or a drive letter already says where it begins, so Combine drops what came before it.
Combine discards earlier pieces when a later one is rooted and throws on null. Join keeps every piece, treats null as empty, and only inserts a separator when one is missing. Join exists in .NET Core 2.1 and newer.
No. Combine("a", "b") returns a/b with no slash at the end. If you need one, add it yourself. Use Path.TrimEndingDirectorySeparator when you want to remove one.
The plus sign needs you to type the separator, and the right separator differs between Windows and Linux. Combine picks it for you and avoids doubled separators when a piece already ends with one.