How do I get .NET's Path.Combine to convert forward slashes to backslashes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Path.Combine in .NET does not convert forward slashes (/) to backslashes (\). If one of the input strings contains forward slashes, they remain in the output as-is. To normalize path separators, call Path.GetFullPath() on the result (on Windows this converts forward slashes to backslashes) or manually replace / with Path.DirectorySeparatorChar. Understanding how Path.Combine handles separators prevents cross-platform path bugs.
The Problem
Path.Combine inserts the platform separator between segments but does not modify separators within segments.
Fix 1: Path.GetFullPath()
Path.GetFullPath() resolves the full path and normalizes separators on Windows:
On Linux/macOS, Path.GetFullPath() keeps forward slashes since / is the native separator.
Fix 2: String.Replace()
Explicitly replace forward slashes with the platform-appropriate separator:
For cross-platform code, replace both separators with the platform one:
Fix 3: Split and Recombine
Split the path by both separators and recombine using Path.Combine:
This also removes empty segments and handles edge cases cleanly.
How Path.Combine Actually Works
The critical gotcha: if any segment starts with / or \, Path.Combine discards all previous segments.
Cross-Platform Best Practices
On Windows, both / and \ work in most APIs. On Linux, only / works. Write code that uses Path.Combine and Path.DirectorySeparatorChar to stay portable.
.NET 6+ Path Helpers
Path.Join is safer than Path.Combine when you do not want rooted segments to reset the path.
Common Pitfalls
- Expecting
Path.Combineto normalize separators: It does not.Path.Combine("a", "b/c")producesa\b/con Windows. CallPath.GetFullPath()or replace manually afterward. - Second argument starting with
/or\:Path.Combine("C:\\base", "/relative")returns/relative, discarding the base path entirely. Trim leading separators from relative segments or usePath.Joininstead. - Hardcoding
\\in paths: This breaks on Linux/macOS. UsePath.CombineorPath.DirectorySeparatorCharto build paths portably. - Using
Path.GetFullPathon relative paths:Path.GetFullPath("docs/file.txt")resolves relative to the current working directory, which may not be what you expect. Pass an absolute base path when possible. - Assuming URI paths and file paths use the same separators: URIs always use forward slashes (
https://example.com/path). Converting a URI path to a file path requires replacing/with the platform separator. Do not usePath.Combinefor URIs — use theUriclass instead.
Summary
Path.Combinedoes not convert forward slashes to backslashes (or vice versa)- Use
Path.GetFullPath()to normalize separators on the current platform - Use
string.Replace('/', Path.DirectorySeparatorChar)for explicit conversion - Watch for segments starting with
/or\—Path.Combinetreats them as absolute and discards prior segments - Use
Path.Join(.NET Core 3.0+) as a safer alternative that preserves all segments - Always use
Path.CombineorPath.DirectorySeparatorCharinstead of hardcoded separator characters

