I was wondering about the difference between \
and /
in file paths. I have noticed that sometimes a path contains /
and sometimes it is with \
.
It would be great if anyone can explain when to use \
and /
.
I was wondering about the difference between \
and /
in file paths. I have noticed that sometimes a path contains /
and sometimes it is with \
.
It would be great if anyone can explain when to use \
and /
.
/
is the path separator on Unix and Unix-like systems. Modern Windows can generally use both \
and /
interchangeably for file paths, but Microsoft has advocated for the use of \
as the path separator for decades.
This is done for historical reasons that date as far back as the 1970s, predating Windows by over a decade. In the beginning, MS-DOS (the foundation to early Windows) didn't support directories. Unix had directory support using the /
character since the beginning. However, when directories were added in MS-DOS 2.0, Microsoft and IBM were already using the /
character for command switches, and because of DOS's lightweight parser (descended from QDOS and designed to run on lower end hardware), they chose to use a new character rather than risk breaking compatibility with their existing applications.
So, to avoid errors about "missing a switch" or "invalid switch" when passing file paths as arguments to commands such as these:
cd/ <---- no switch specified
dir folder1/folder2 <---- /folder2 is not a switch for dir
it was decided that the \
character would be used instead, so you could write those commands like this
cd\
dir folder1\folder2
without error.
Later, Microsoft and IBM collaborated on an operating system unrelated to DOS called OS/2. OS/2 had the ability to use both separators, probably to attract more Unix developers. When Microsoft and IBM parted ways in 1990, Microsoft took what code they had and created Windows NT, on which all modern versions of Windows are based, carrying this separator agnosticism with it.
As backward compatibility has been the name of the game for Microsoft from all of the major OS transitions that they've undertaken (DOS to Win16/DOS, to Win16/Win32, to Win32/WinNT), this peculiarity stuck, and it will probably exist for a while yet.
It's for this reason that this discrepancy exists. It should really have no effect on what you're doing because, like I said, the Windows API can generally use them interchangeably. However, 3rd party applications (and even many of Microsoft's own for legacy reasons) may break if you pass a /
when they expect a \
between directory names. If you're using Windows, stick with \
. If you're using Unix or URIs (which have their foundation in Unix paths, but that's another story entirely), then use /
.
foo.exe /bar
might be interpreted as a command-line switch, while foo.exe \bar
might be interpreted as referring to a file/folder called bar
which is located in the root directory \
of the current "drive", like C:\
for example.
Commented
Jul 18, 2016 at 9:05
/
to \
is done in the Win32 compat layer, meaning that if you circumvent it, there will be a difference. The best known example of this are extended length paths: \\?\C:\
will work as expected on NTFS but \\?\C:/
won't.
/
and ` is not entirely true. For network path you have to use
` (eg. \\<servername> bot not //<servername>)
MS-DOS 1.0 retained the command line option (or switch) character convention of '/' from CP/M. At that time there was no directory structure in the file system and no conflict.
When Microsoft developed the more Unix like environment with MS-DOS (and PC-DOS) 2.0, they needed to represent the path separator using something that did not conflict with existing command line options. Internally, the system works equally well with either '/' or '\'. The command processor (and many applications) continued to use the '/' as a switch character.
A CONFIG.SYS
entry SWITCHAR=-
could be used to override the /
default to improve Unix compatibility. This makes built in commands and standard utilities use the alternate character. The Unix path separator could then be unambiguously used for file and directory names. This entry was removed in later versions, but a DOS call was documented to set the value after booting.
This was little used and most third-party tools remained unchanged. The confusion persists. Many ports of Unix tools retain the '-' switch character while some support both conventions.
The follow-on PowerShell command processor implements rigorous escaping and switch parameters and largely avoids the confusion except where legacy tools are used.
Neither the question nor the answer relate to C#.
/
as an option introducer in various PDP-11 operating system such as RSTS (1970) and RSX (1972) precedes that in CP/M (1973).
\
is correct in a Windows file
path and /
is correct in a URI.This can be relevant resource.
\
to /
automatically. In my book this is called "works seamlessly".
On Unix-based systems \
is an escape character, that is, \
tells the parser that this is a space and not the end of the statement. On Unix systems /
is the directory separator.
On Windows \
is the directory separator, but the /
cannot be used in file or directory names.
\
and /
(as well several other symbols) can't be used in filenames because DOS didn't have the same complex parser that Unix users are so used to. The lack of a good parser was the result of MS-DOS being descended from QDOS ("Quick and Dirty Operating System"). It was meant to get things running quickly and on limited hardware. All this of course still exists in the present day for backward compatibility.
Commented
Jul 18, 2016 at 6:10
/
was added as an "Alternate_Directory_Separator"
You shouldn't be using either in C#. You should always use the Path
class. This contains a method called Path.Combine
that can be used to create paths without specifying the separator yourself.
Example usage:
string fullPath = System.IO.Path.Combine("C:", "Folder1", "Folder2", "file.txt");
Apart from the answers given, it is worth mentioning that \
is widely used for special characters (such as \n
\t
) in programming languages, text editors and general systems that apply lexical analysis.
If you are programming for instance, it is inconvenient at times to need to even need to escape backslash with another one (\\
) in order to use it properly - or need to use escaping strings, such as C# @"\test"
.
Of course, as mentioned before, web URIs use forward slash by standard but both slashes work in the latest and most common command line tools.
UPDATE: After searching a little bit, it seems out the whole story between /
and \
goes back in "computer history", in the ages of DOS and the Unix-based systems at that time. HowToGeek has an interesting article about this story.
In short terms, DOS 1.0 was initially released by IBM with no directory support, and /
was used for another ("switching") command functionality. When directories were introduced in 2.0 version, /
was already in use, so IBM chose the visually closest symbol, which was \
. On the other hand, Unix standardly used /
for directories.
When users started to use many different systems, they started becoming confused, making the OS developers to attempt making the systems work in both cases - this even applies in the part of URLs, as some browsers support the http:\\www.test.com\go format. This had drawbacks though in general, but the whole thing stands today still for backward compartibility causes, with an attempt for support of both slashes on Windows, even though they are not based on DOS anymore.
` as well as many
make` shells... you are right that recent Windows have defined the ALTERNATE_PATH_SEPARATOR environment variable which defaults to /
hence Windows can probably accept both.
/
paths everywhere in the system - of course, applications could misunderstand those paths at their leisure, so it wasn't used too much. Non-CLI applications that didn't try to do their own (broken) validation of paths worked fine from the get go.
\
is used for Windows local file paths and network paths as in:
C:\Windows\Temp\
or \\NetworkSharedDisk\Documents\Archive\
/
is what is required by standard URIs as in:
/
in standarad URIs as I've stated in the answer.