C# Path Join() Method

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

Definition

In C#, Path.Join() glues path pieces together with the system separator. It looks only at the boundary between two pieces: if neither the first piece ends with a separator nor the second starts with one, it inserts one. Every piece is kept.

That is the key difference from Combine: Join has no rooted rule. Join("a", "/b") is a/b, not /b. It also treats null as an empty string instead of throwing. Join is available in .NET Core 2.1 and newer (and .NET Standard 2.1).

This is a static method of System.IO.Path. Call it as Path.Join(...); 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 to four pieces, or an array. Pieces can be strings or ReadOnlySpan<char>.

C#
string path = Path.Join("data", "reports", "2026.txt");

Console.WriteLine(path.Replace('\\', '/'));          // data/reports/2026.txt
Console.WriteLine(Path.Join("a/", "/b"));            // a//b  (nothing is cleaned up)
CallWhat it does
Join(a, b)Inserts a separator only if the boundary has none.
Join(a, b, c)Three pieces. There is also a four-piece form and a params form in newer .NET.
Join("a", "/b")Gives a/b. The second piece starts with a separator, so none is added.
Join("a/", "/b")Gives a//b. Both sides have a separator, and Join does not remove either.

Join does not normalize. It does not remove doubled separators, resolve .., or discard anything. If you need a clean, full path afterwards, pass the result to Path.GetFullPath.

Library declarations
C#
public static string Join(string? path1, string? path2);
public static string Join(string? path1, string? path2, string? path3);
public static string Join(string? path1, string? path2, string? path3, string? path4);
public static string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);

Parameters

ParameterDescription
path1, path2, ...The pieces to glue. A null piece counts as empty. Rooted pieces get no special treatment.

Return Value

Return ValueDescription
stringAll the pieces in order, with a separator added at each boundary that does not already have one. If one piece is empty, the other is returned unchanged.

C# Examples

Example 1 — Glue Folders and a File Name

The ordinary case: pieces without separators get one between them, and an existing separator is respected.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string path = Path.Join("folder", "sub", "file.txt");
        Console.WriteLine(path.Replace('\\', '/'));

        Console.WriteLine(Path.Join("data/", "file.txt"));
        Console.WriteLine(Path.Join("data", "/file.txt"));
        Console.WriteLine(Path.Join("data", "").Replace('\\', '/'));
    }
}

How It Works

  1. Separators are inserted. Neither folder nor sub ends with one, so Join adds the system separator. On Windows that is a backslash, which Replace('\\', '/') turns into a slash for printing.
  2. A trailing separator is enough. "data/" already ends with one, so nothing is inserted before file.txt.
  3. A leading separator is enough, too. "/file.txt" starts with one. The pieces are glued as they are, and the result is the same text as the line above.
  4. An empty piece changes nothing. When the second piece is empty, Join returns the first one, data, with no separator added after it.

Example 2 — Join and Combine on the Same Input

Give both methods the same pieces, including a rooted one and a double-separator case, and compare the answers.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine("Combine: " + Path.Combine("uploads", "/etc/passwd"));
        Console.WriteLine("Join:    " + Path.Join("uploads", "/etc/passwd"));

        Console.WriteLine("Both ends have a slash: " + Path.Join("a/", "/b"));
        Console.WriteLine("Combine with the same: " + Path.Combine("a/", "/b"));
    }
}

How It Works

  1. Combine follows the rooted piece. /etc/passwd starts with a separator, so the answer is that piece alone and uploads is gone.
  2. Join keeps both. The same two pieces are glued without a second separator, giving uploads/etc/passwd. The rooted rule simply does not exist in Join.
  3. Double separators survive. "a/" ends with a separator and "/b" starts with one, so Join leaves both and prints a//b.
  4. Combine sees a rooted piece again. "/b" is rooted, so the result is just /b. Same input, different rules, which is why you choose on purpose.

Example 3 — null Is Treated as Empty

Join is forgiving with null. This helps with optional parts, but it can also hide a bug.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string missing = null;

        Console.WriteLine(Path.Join("a", missing));
        Console.WriteLine(Path.Join(missing, "b"));
        Console.WriteLine(Path.Join("", "") == "");

        string folder = "backup";
        string subFolder = null;
        string result = Path.Join(folder, subFolder, "db.bak");
        Console.WriteLine(result.Replace('\\', '/'));
    }
}

How It Works

  1. null on the right. Join("a", missing) gives a. The null piece is treated as an empty string, and no exception is thrown.
  2. null on the left. Join(missing, "b") gives b. With an empty first piece, the second piece is returned as it is.
  3. All empty. Two empty strings give an empty string, so the comparison with "" is True.
  4. An optional middle piece. subFolder is null, so only backup and db.bak remain, with a single separator between them. Be careful: a typo that produces null will not be reported.

Frequently Asked Questions

It glues path pieces together and inserts the system separator at each boundary that does not already have one. It keeps all pieces and only builds text; it does not touch the disk.
Use Combine when a rooted later piece should win, for example a default path that the user can override. Use Join when you want plain glue that never drops a piece. For untrusted input, check the piece first with either one.
Join only checks whether a separator is missing. If the first piece ends with one and the second starts with one, both stay, so Join("a/", "/b") is "a//b". Trim one side first, for example with Path.TrimEndingDirectorySeparator.
No. A null piece is treated as an empty string, so Join("a", null) is "a" and Join(null, "b") is "b". Path.Combine throws ArgumentNullException for the same input.
.NET Core 2.1 and newer, and .NET Standard 2.1. It is not in the old .NET Framework Path class, so older projects must use Combine instead.

Next: TrimEndingDirectorySeparator()

Remove one trailing separator from a path, but never from a root such as / or C:\.

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

7 people found this page helpful