Using / or \\ for folder paths in C# Using / or \\ for folder paths in C# windows windows

Using / or \\ for folder paths in C#


Windows supports both path separators, so both will work, at least for local paths (/ won't work for network paths). The thing is that there is no actual benefit of using the working but non standard path separator (/) on Windows, especially because you can use the verbatim string literal:

string path = @"C:\"  //Look ma, no escape

The only case where I could see a benefit of using the / separator is when you'll work with relative paths only and will use the code in Windows and Linux. Then you can have "../foo/bar/baz" point to the same directory. But even in this case is better to leave the System.IO namespace (Path.DirectorySeparatorChar, Path.Combine) to take care of such issues.


I write paths in C# like this:

@"C:\My\Path"

The @ character turns off \ escaping.

EDIT a decade later

.NET now runs on Linux. Use Path.Combine() where feasible, otherwise use Path.DirectorySeparatorChar to construct a path with \ or / as appropriate to the underlying OS.