Path.Combine
dot notation
C#
programming
code duplication

Path.Combine and the dot notation

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In .NET, Path.Combine is the right tool for joining path segments, but it is easy to misunderstand what happens when one of those segments is "." or "..". The short version is that Path.Combine mainly concatenates path parts safely. It does not fully normalize the path for you.

That distinction matters because many developers expect Path.Combine(basePath, "..", "file.txt") to immediately resolve to the final canonical path. In practice, you often need Path.GetFullPath after combining if you want dot segments collapsed.

What Path.Combine actually does

Path.Combine joins segments using the correct directory separator for the platform.

csharp
1using System;
2using System.IO;
3
4string path = Path.Combine("logs", "2026", "app.txt");
5Console.WriteLine(path);

This avoids brittle string concatenation such as "logs/" + year + "/app.txt".

It also handles separators more safely when one segment already ends with a slash. That is the real purpose of Path.Combine.

How "." and ".." behave

The "." segment means "current directory" and ".." means "parent directory". When you pass them into Path.Combine, they are kept as path segments:

csharp
1using System;
2using System.IO;
3
4string combined = Path.Combine("/var/app/data", "..", "config.json");
5Console.WriteLine(combined);

The output will look like:

text
/var/app/data/../config.json

That path is valid, but it is not normalized yet. The ".." is still present in the string.

Normalize with Path.GetFullPath

If you want the final resolved path with dot segments collapsed, call Path.GetFullPath after combining:

csharp
1using System;
2using System.IO;
3
4string combined = Path.Combine("/var/app/data", "..", "config.json");
5string fullPath = Path.GetFullPath(combined);
6
7Console.WriteLine(fullPath);

This is the important pattern:

csharp
string path = Path.GetFullPath(Path.Combine(basePath, "..", "config.json"));

Path.Combine builds the path safely. Path.GetFullPath resolves . and ...

Rooted paths can reset the result

Another detail that surprises people is that if a later segment is rooted, it can replace the earlier parts.

csharp
1using System;
2using System.IO;
3
4string path = Path.Combine("C:\\app", "data", "D:\\other.txt");
5Console.WriteLine(path);

The rooted D:\other.txt takes over. So Path.Combine is not simply "append every segment in order no matter what." Rooted segments change the outcome.

This is especially important when some path parts come from configuration or user input.

A safe everyday pattern

If you are building a path relative to the current project or executable directory, keep the flow explicit:

csharp
1using System;
2using System.IO;
3
4string baseDirectory = AppContext.BaseDirectory;
5string filePath = Path.GetFullPath(
6    Path.Combine(baseDirectory, "..", "data", "seed.json")
7);
8
9Console.WriteLine(filePath);

This is readable and handles separators and dot segments correctly.

It also makes intent obvious during code review. One method call combines segments safely, and the second method call clearly signals that you want path normalization rather than mere concatenation.

Common Pitfalls

The most common mistake is assuming Path.Combine automatically resolves "." and ".." into a final canonical path. It usually does not. Use Path.GetFullPath when you need normalization.

Another issue is confusing path dot notation with object-property dot notation. In this context, "." and ".." are filesystem path markers, not member-access syntax.

Developers also forget that a rooted later segment can override the earlier combined path. If one input path is absolute, the final result may ignore earlier segments.

Finally, do not build paths by hand with string concatenation when Path.Combine already handles separator rules for you.

Summary

  • 'Path.Combine safely joins path segments but does not fully normalize dot segments by itself.'
  • '"." and ".." remain in the combined path until you resolve them.'
  • Use Path.GetFullPath after Path.Combine when you want a canonical path.
  • Be aware that rooted later segments can replace earlier path parts.
  • Prefer Path.Combine over manual string concatenation for filesystem paths.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.