Path.Combine absolute with relative path strings
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Path.Combine in .NET is simple until one of the later path segments is already absolute. At that point, Path.Combine does not append to the earlier path. Instead, it discards the previous segments and returns a path rooted at the absolute argument. That behavior is correct, but it surprises a lot of people.
How Path.Combine Actually Works
Path.Combine is meant to join path segments safely, inserting directory separators where needed. The key rule is:
If any later argument is rooted, earlier parts are ignored from that point forward.
A small example:
The first call produces a combined path under C:\app\data. The second call returns the D:\archive\output.txt path because the second argument is rooted.
Why This Behavior Exists
Once .NET sees a rooted path, it assumes that segment defines the full path context and that the earlier base path is no longer relevant.
That is useful because it lets code safely handle both cases:
- a user supplied a relative child path
- a user supplied a complete absolute path
The function is not trying to "force append" absolute paths. It is trying to produce a valid path interpretation.
Check Rooted Paths Explicitly
If your logic depends on whether the second part is relative or absolute, check that first.
This makes the rule explicit instead of relying on callers to know Path.Combine semantics.
Use Path.GetFullPath for Normalization
If you want to resolve a relative path against a base directory and then normalize the result, Path.GetFullPath is often the better tool.
This is especially useful when relative segments such as .. are involved.
Path.Combine Versus Path.Join
Newer .NET also includes Path.Join. It is useful for joining segments without some of the validation overhead of Path.Combine, but it does not change the main conceptual issue: rooted paths still need deliberate handling in your logic.
In most application code:
- use
Path.Combinewhen you want standard path semantics - use
Path.Joinwhen you care about lower-level joining behavior and already know what the inputs mean
Do not treat either as a generic string concatenation tool.
Cross-Platform Notes
On Windows, rooted paths often begin with a drive letter, such as C:\, or a UNC path. On Unix-like systems, a rooted path starts with /.
That means Path.IsPathRooted is the right cross-platform check:
Avoid writing path rules that assume only Windows drive-letter syntax unless the application is truly Windows-only.
Safe Wrapper for User Input
If user input may be either relative or absolute, a small wrapper keeps the policy clear.
This still needs security review if the input comes from untrusted users, because a normalized path can still escape a base directory if you allow it.
Common Pitfalls
One common mistake is assuming Path.Combine(basePath, absolutePath) appends the absolute path to the base path. It does not.
Another mistake is using plain string concatenation for file paths, which breaks separator handling and cross-platform behavior.
Developers also forget to normalize results when .. segments are present, leading to confusing relative path output.
Finally, some code accepts user input as a child path but forgets to reject rooted paths, which can break storage isolation rules.
Summary
- '
Path.Combineignores earlier segments once it encounters a rooted path.' - This behavior is intentional and usually correct.
- Use
Path.IsPathRootedwhen your code needs to treat absolute and relative inputs differently. - Use
Path.GetFullPathto normalize combined paths. - Do not use path APIs as simple string concatenation helpers.
Related reading
- Path.Combine and the dot notation
- Path.Combine for URLs?
- Path.Combine for URLs?
- Patterns for Multithreaded Network Server in C
- Pause and Resume Subscription on cold IObservable
- Performance of direct virtual call vs. interface call in C
- Performance of Find vs. FirstOrDefault
- Performance of ReceiveAsync vs. BeginReceive

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.