Use Path.Combine when a later rooted segment should replace the earlier path. Use Path.Join when you want to concatenate path segments without that reset. Both return strings; neither proves that the result names an existing file. Microsoft documents this distinction in the Path.Join reference.
This tutorial uses .NET 10 and a single console program with sixteen assertions. It creates no files or directories. The fixture was compiled and run on Linux; Windows expectations are embedded in the program for readers to run locally.
String work still follows operating system rules
System.IO.Path lives in an I/O namespace, but most of its members manipulate path strings without checking the filesystem. Changing a string with ChangeExtension does not rename a file. The platform determines path syntax and the value of DirectorySeparatorChar, so the program constructs inputs using that separator and chooses expected strings with OperatingSystem.IsWindows(). See the Path class remarks and member descriptions.
The important exception in this example is GetRelativePath: it internally calls GetFullPath on both arguments before computing their difference. Relative arguments can therefore depend on the current directory. Every call below instead receives fully qualified paths: a drive-qualified path on Windows or a slash-rooted path on Unix. No directory named sandbox has to exist. The first argument is treated as a directory, including when it has no trailing separator. These rules come from the GetRelativePath reference.
Run the sixteen-case program
With the .NET 10 SDK installed, create a console project:
dotnet new console --framework net10.0 --name PathCases
cd PathCases
Replace Program.cs with this complete program. Each row contains an operation and an exact expected string. Square brackets in the output make empty results visible. A mismatch prints FAIL and the expected value; the final check throws if any assertion fails.
using System;
using System.IO;
bool windows = OperatingSystem.IsWindows();
string separator = Path.DirectorySeparatorChar.ToString();
string root = windows ? @"C:\sandbox" : "/sandbox";
string alpha = root + separator + "alpha";
string beta = root + separator + "beta";
string leaf = root + separator + "leaf";
string W(string onWindows, string onUnix) => windows ? onWindows : onUnix;
var cases = new (string Name, Func<string> Run, string Expected)[]
{
("combine relative", () => Path.Combine("base", "report.txt"),
W(@"base\report.txt", "base/report.txt")),
("join relative", () => Path.Join("base", "report.txt"),
W(@"base\report.txt", "base/report.txt")),
("combine rooted", () => Path.Combine("base", leaf),
W(@"C:\sandbox\leaf", "/sandbox/leaf")),
("join rooted", () => Path.Join("base", leaf),
W(@"base\C:\sandbox\leaf", "base/sandbox/leaf")),
("combine trailing separator", () => Path.Combine("base" + separator, "report.txt"),
W(@"base\report.txt", "base/report.txt")),
("join duplicate separators", () => Path.Join("base" + separator, separator + "leaf"),
W(@"base\\leaf", "base//leaf")),
("combine empty first", () => Path.Combine("", "report.txt"), "report.txt"),
("join both empty", () => Path.Join("", ""), ""),
("extension double suffix", () => Path.GetExtension("archive.tar.gz"), ".gz"),
("extension dotfile", () => Path.GetExtension(".gitignore"), ".gitignore"),
("filename trailing separator", () => Path.GetFileName("base" + separator), ""),
("filename without extension", () => Path.GetFileNameWithoutExtension("archive.tar.gz"),
"archive.tar"),
("change last extension", () => Path.ChangeExtension("archive.tar.gz", ".zip"),
"archive.tar.zip"),
("relative child", () => Path.GetRelativePath(alpha, alpha + separator + "report.txt"),
"report.txt"),
("relative sibling", () => Path.GetRelativePath(alpha, beta + separator + "report.txt"),
W(@"..\beta\report.txt", "../beta/report.txt")),
("relative same directory", () => Path.GetRelativePath(alpha, alpha), ".")
};
int passed = 0;
for (int i = 0; i < cases.Length; i++)
{
var test = cases[i];
string actual = test.Run();
bool ok = actual == test.Expected;
if (ok) passed++;
Console.WriteLine($"{(ok ? "PASS" : "FAIL")} {i + 1:00} {test.Name} = [{actual}]");
if (!ok) Console.WriteLine($" expected [{test.Expected}]");
}
Console.WriteLine($"{passed}/{cases.Length} passed");
if (passed != cases.Length) throw new InvalidOperationException("Path assertion failed.");
Run it from the project directory:
dotnet run
The verified Linux output is:
PASS 01 combine relative = [base/report.txt]
PASS 02 join relative = [base/report.txt]
PASS 03 combine rooted = [/sandbox/leaf]
PASS 04 join rooted = [base/sandbox/leaf]
PASS 05 combine trailing separator = [base/report.txt]
PASS 06 join duplicate separators = [base//leaf]
PASS 07 combine empty first = [report.txt]
PASS 08 join both empty = []
PASS 09 extension double suffix = [.gz]
PASS 10 extension dotfile = [.gitignore]
PASS 11 filename trailing separator = []
PASS 12 filename without extension = [archive.tar]
PASS 13 change last extension = [archive.tar.zip]
PASS 14 relative child = [report.txt]
PASS 15 relative sibling = [../beta/report.txt]
PASS 16 relative same directory = [.]
16/16 passed
Read the cases that change your choice of API
Rows 1 and 2 agree because both segments are relative. Rows 3 and 4 expose the difference: Combine drops base when the next segment is rooted; Join retains it. On Windows, the fourth row deliberately produces base\C:\sandbox\leaf. That is a concatenated string, not a usable drive-qualified destination. On Linux it produces base/sandbox/leaf. Concatenation alone does not validate a destination. The Join documentation also demonstrates this possibility.
Rows 5 and 6 show that adding separators is different from normalizing them. A trailing separator already supplies the boundary. If both adjoining strings supply one, Join preserves both: the sixth row yields base//leaf on Linux or two consecutive backslashes on Windows. Choose input segments deliberately; do not assume joining cleans them up.
Rows 7 and 8 cover empty strings. Combining an empty first segment with a filename returns that filename; joining two empty strings returns an empty string. These checks say nothing about null inputs or whitespace-only inputs, which this fixture does not test.
Extensions are string suffixes
The tested double-extension case treats .gz as the extension of archive.tar.gz. Removing that extension leaves archive.tar, and changing it to .zip gives archive.tar.zip. Path methods do not infer that .tar.gz represents a compression format or inspect the contents of an archive.
The dotfile row is another useful surprise: GetExtension returns .gitignore for the name .gitignore. A leading dot does not make this particular name extensionless to the API. The trailing-separator row returns an empty filename. These are the exact results asserted above, using the extraction and extension operations described in the Path member reference.
Keep relative-path calculations independent of the working directory
The last three rows compute a child path, a sibling path and the path back to the same directory. Their results are report.txt, a parent step followed by beta and the filename, and .. The sibling result contains the platform’s directory separators.
GetRelativePath compares paths using ordinal case-insensitive comparison on Windows and macOS, and ordinal case-sensitive comparison on Linux. The fixture uses consistent casing throughout, so its Windows/Unix expectation split does not conceal a macOS casing difference. See Microsoft’s platform comparison rules. Windows and macOS runs were not part of the container verification.
For this exercise, avoid directly calling GetFullPath with relative inputs: that introduces current-directory dependence. Also avoid GetTempFileName, which actually creates a zero-byte file on disk. The Path class reference describes that disk-writing behavior. These are different concerns: resolving a path string against the environment is not the same as creating a file. Supplying fully qualified inputs to GetRelativePath avoids the former dependency in our cases.
Use this program to check path-string behavior before adding file access. If your next step is actual I/O, continue with reading and writing text files. For general text operations, see joining and splitting strings; those operations do not apply path-specific rooted-segment rules.