views:

731

answers:

8

it seems windows insist on writing this slash on files' path \, while .net's URI class writing this the other way?

Is there any right way? that is accepted even in the most primitive systems?

And why the .net's URI is showing the other slash than windows does?

Thanks.

+2  A: 

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
+5  A: 

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
+17  A: 

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
Does Internet Explorer accept backslashes in place of slashes?
BryanH
BryanH: It does, although it is incorrect. See: http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx
Ben James
+1 for *Windows is the bastard child of operating systems* :)
voyager
Nice answer from Jed and nice pointer to the IEBlog article on the Hell that is file URIs on Windows from Ben.
Michael Burr
@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
"M$" makes me tired.
Jed Smith
+1 for the bastard child as well, but with the famous additional quote "those who don't understand Unix are doomed to reinvent it. badly".
Stefano Borini
+1  A: 

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.

EmFi
+3  A: 

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

Rubens Farias
a current *file* separator*. and btw, when ever is this not slash?
Itay
@Itay: _Directory_ SeparatorChar, so *path* separator =)Maybe running on *nix this things change a bit
Rubens Farias
+4  A: 

The reason for ths is a little peice 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, 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 an other 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 where out there way before Microsoft implemented directory support into their DOS

andremo
@grombeestje: I'd think that UNIX took `/` from MULTICS :)
voyager
yeah, history goes even further back, indeed.
andremo
+1  A: 

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
So we are forever stuck with an incompatible implementation with every other system decision from Redmond (where they there at the time?) for sake of backward compatibility (where have I heard this story before?).
voyager
Well - Redmond actually inherited the problem from CP/M where the use of '/' for command options in MS-DOS came from. As far as the problems that backward compatibility cause, they're real and I imagine that MS would like to be able to often ignore backward compatibility, but for the most part they'd rather have the headaches than irritate customers.
Michael Burr
A: 

\ 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