82

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Letterman
  • 4,076
  • 5
  • 29
  • 41
  • 43
    The reason Microsoft is backwards on this goes back to MS-DOS 2.0 (DOS 1.0 had no directory hierarchy), which used a backslash to stay compatible with Dos 1.0 commands, which used slash for command line switches. Horrible, isn't it? – Nosredna Oct 19 '09 at 17:48
  • 12
    And slash for command line switches is a heritage from CP/M. – starblue Oct 19 '09 at 17:57
  • 2
    Possible duplicate of [Difference between '/' and '\' in file path](http://stackoverflow.com/questions/38428561/difference-between-and-in-file-path) – Florian Koch Jul 20 '16 at 06:35

12 Answers12

101

Windows is the bastard child of operating systems in this regard, but a lot of APIs will accept forward slashes as well. On Windows, a file path looks like this:

C:\Users\jsmith\Documents\file.txt

On a Unix-like system (including Mac OS X and Linux), the same path would look like this:

/home/jsmith/Documents/file.txt

A URL, standardized in RFC 1738, always uses forward slashes, regardless of platform:

http://home.example.com/Documents/file.txt

The reason for this is historical. Not even Windows can reverse our thinking on URLs. When you're talking about backslashes, the only platform you'll find that uses them is Windows (and some other novelty ones).

Where you might see backslashes used other than Windows would be UNC paths -- however, Windows is the chief proponent of these as well:

\\HOMESVR\Documents\file.txt

And whatever you do, don't make a commercial for your Web site and say "my company dot com back slash promotion".

Jed Smith
  • 15,584
  • 8
  • 52
  • 59
  • 3
    Does Internet Explorer accept backslashes in place of slashes? – BryanH Oct 19 '09 at 17:20
  • 4
    BryanH: It does, although it is incorrect. See: http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx – Ben James Oct 19 '09 at 17:21
  • 1
    @Bryan: ie also does something with ( encoded ) %40 that is directly the opposite of what the rfc says - spec says / has to be the separator between the host - resource. I did some digging on this recently and found that at some level the whole M$ thing resolves on keeping everyone happy and so it is likely that what msie does with "/" vs "\" will be unpredictable. – Nicholas Jordan Oct 20 '09 at 01:30
  • 1
    This isn't exactly correct. Windows accepts `/` and `\` in paths. – Timmmm Jan 24 '20 at 14:07
  • 1
    Windows cmd shell has some curious behaviour here - type `dir "C:/Windows/System32/"` to see a long list of files, including `cmd.exe`, and then observe that `dir "C:/Windows/System32/cmd.exe"` doesn't work. Weird. – yoyo Dec 18 '20 at 01:59
  • @yoyo: ... doesn't work in cmd (but in PowerShell). Nice catch anyway. – Rainald62 Oct 12 '22 at 15:46
  • It's not exactly the "bastard child" of operating systems. There does exist a world outside of Unix and Windows and OSes such as OS/2, DOS, CP/M all used a backslash as a directory separator in the versions that supported directories. – PC Luddite Jul 06 '23 at 07:50
75

A file path and a URI are different. \ is correct in a Windows file path and / is correct in a URI.

So this file path: C:\Documents\Foo translates to this URI: file:///C:/Documents/Foo

Ben James
  • 121,135
  • 26
  • 193
  • 155
  • 7
    It bears mentioning that backslash `/` is the _string escape character_ in several languages… using a backslash in a hard-coded string necessitates an extra backslash; e.g., `var foo = "C:\\Documents\\Foo";`. – Matthemattics Sep 30 '13 at 14:13
  • Also, several browsers (namely, Firefox & Opera) fail catastrophically when encountering URIs with backslashes. – Matthemattics Sep 30 '13 at 14:16
  • 17
    Not true. `file:///C:\Documents\Foo` works just fine – Borodin Aug 04 '16 at 22:28
  • 3
    Windows is quite permissive when you try to access files with "file:///" It allows file:///C:\Documents\Foo, file:///C:/Documents/Foo, and mixed examples like file:///C:/Documents\Foo. And you can vary the case of the alphabetic characters. – ProfDFrancis Dec 04 '19 at 13:16
  • 7
    This answer is completely wrong. Not only does `file:///C:\Documents\Foo` work, but so does `C:/Documents/Foo`. – Timmmm Jan 24 '20 at 13:59
  • 1
    @Timmmm Both of these are incorrect though and will work only in some cases "as a courtesy" from some function or program. – CherryDT Mar 21 '23 at 12:19
52

The reason for this is a little piece of history. When UNIX was created, or should I rather say UNICS, they chose the / as separator for directories. Back in the days, storage media was rather small, and every directory in the root was another mounted storage device (/bin /lib etc.)

When Microsoft release MS-DOS version 1.0, it did not have directory support. They used the / character for parameters from programs (program /a /b)

MS-DOS 1.0, a quick rebrand of Q-DOS, is a CP/M derived operating system, from which it inherited drive letters (A: C: etc.)

As in later versions they wanted to add some directory support, they chose to use the \ since the / already had another meaning in their operating system.

There are many artifacts of computer history in modern operating systems, which I suppose most people don't realize, but still have a major influence on how they work.

So, what is the right way? If there is any, I would say it's the / because UNIX-like operating systems were out there way before Microsoft implemented directory support into their DOS.

mklement0
  • 382,024
  • 64
  • 607
  • 775
18

As a side note and talking about .NET, you should use System.IO.Path.DirectorySeparatorChar to get the current path separator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • a current *file* separator*. and btw, when ever is this not slash? – Letterman Oct 20 '09 at 16:51
  • @Itay: _Directory_ SeparatorChar, so *path* separator =) Maybe running on *nix this things change a bit – Rubens Farias Oct 20 '09 at 16:57
  • 2
    Now that we have .NET Core (and presumably always on Mono), `[System.IO.Path]::DirectorySeparatorChar` indeed reports `/` on Unix platforms. The property name is unfortunate, however, especially given that there's also `[System.IO.Path]::PathSeparator`, which reports the (platform-specific) char. used to separate entries in the `PATH` environment variable (`;` vs. `:`). Also note that there's `[System.IO.Path]::AltDirectorySeparatorChar`, which reports `/` also on Windows to reflect the fact that you can use `/` even on Windows. – mklement0 Apr 08 '17 at 16:49
17

As far as file system path separators go, I believe that on Windows all APIs will accept forward slashes (but perhaps there are some buggy ones that don't) - the problem is that most applications don't accept them (or parse them incorrectly).

In fact, if I recall correctly, even MS-DOS accepted '/' as a path separator at the API level ever since it started supporting subdirectories (v2.0) - but by that time the '/' character had already been established as the 'switch' character for command line options, so the backslash became the defacto path separator on DOS (and later Windows).

URIs are a similar but different animal from file paths, and URIs should always use '/' to separate components. Windows applications and APIs probably accept '\' as a separator in URIs probably because people are familiar with using backslash as a separator on those systems and URIs can also be use to represent local files.


Useless trivia of the day - in some early versions of MS-DOS there was an API to change the command line option switch character (generally from '/' to '-') so the commands could look more Unix-like and the commands would accept '/' as a path separator on the command line. The API was less than successful (I guess because it wasn't universally supported by applications), and it was removed in later versions.

Hmm... on second reading, this whole answer is pretty much useless trivia.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
11

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...

Greenonline
  • 1,330
  • 8
  • 23
  • 31
6

The web is based on the UNIX way of delimiting directories in a path with a slash (/). Windows separates directories with backslashes (\)

The right way depends on it's use. For a path to a local file on a windows machine, use backslash. For a path to a web resource or file located on a UNIX based machine (includes Macs, Linux), use a slash.

The reason .NET's URI uses forward slashes is because it's formatting for use in a webbrowser.

The server will do all the necessary work to link web resources to files on a hard drive.

bendin
  • 9,424
  • 1
  • 39
  • 37
EmFi
  • 23,435
  • 3
  • 57
  • 68
4

Windows accepts both for path.

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

UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • 1
    which version of Windows? – jxramos Nov 20 '19 at 18:21
  • Probably all of them, but it's been like this since at least Windows XP. – Timmmm Jan 24 '20 at 14:06
  • 1
    Windows NT and later. – P Varga Apr 12 '23 at 06:53
  • @jxramos All Windows NT versions, but it was interchangeable as far back as OS/2. The DOS decended versions of Windows never supported this though, so any program that permitted them would have been app-specific. Well formed Windows programs using the Windows API correctly will support both in all modern versions of Windows. – PC Luddite Jul 06 '23 at 07:38
3

Windows uses the backslash (\) for the file system delimiter. For everything else the forward slash is used (/). The Uri type uses the forward slash because that is how a uniform resource identifier is defined.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
2

\ Backslash is dangerous, since you need to be careful with escaping all the time. Many programming languages have a printf equivalent that uses backslash for escaping.

/ Frontslash is mostly harmless.

: colon was (and to some degree still is) used by Apple.

neoneye
  • 50,398
  • 25
  • 166
  • 151
2

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.

Markus Kuhn
  • 983
  • 9
  • 10
1

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197