So what IS the right direction of the path's slash (/ or \) under Windows?

Viewed 135650

It seems Windows insists on writing a backslash \ in file paths, whereas .NET's URI class writes them with a slash /. Is there any right way, that is accepted even in the most primitive systems? And why is .NET's URI showing the other slash compared with the rest of Windows?

12 Answers

I can confirm: many messages above are false...

  • Windows accepts both / or \

  • Linux accepts only /

It's because when you develop PHP under Windows you use /. It's compatible for both Windows or Linux...

Windows accepts both for path.

Try to open Windows Explorer and type C:/Temp/Foo, c:\Temp\Foo will be correctly opened.

The backslash \ is the actual Windows path-component separator. However, Windows performs a number of path-normalization steps on most paths that it receives via its API. Many of these steps are for compatibility with MS-DOS peculiarities (e.g., removing trailing dot and spaces), and in one of these steps it converts any forward slash / that it encounters into a back slash \. Therefore using the forward slash as a path separator will usually also work, which is very useful (and recommended) when you write code that should run on both Windows and POSIX (Unix/Linux/macOS/etc.) systems. See also the section File path formats on Windows systems in the documentation for a more detailed summary of the path-normalization algorithm.

Also to add to others answers, if you need to build the path, please don’t use backslash or forward slash to form the path based on windows or Linux.

Best way to do this to use Path.Combine.

Here is the documentation.

Related