Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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

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.

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.

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.

Windows accepts both for path.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top