If I recall correctly, backslash is used to denote something within the current computer or network such as C:\Windows or \\172.12. 1.34, while forward slashes are used to denote something that is outside or external to the current computer or network such as http://www.google.com/.
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.
1 Answer. Show activity on this post. Windows ignores double backslashes. So while the second syntax with \ is correct and you should use that one, the first with \\ works too.
There are two types of slashes: a backslash (\) and a forward slash (/). The backslash is used only for computer coding. The forward slash, often simply referred to as a slash, is a punctuation mark used in English.
/
is the path separator on Unix and Unix-like systems. Modern Windows can generally use both \
and /
interchangeably for filepaths, 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, designed to run on lower end hardware), they couldn't find a feasible way to use the /
character without breaking compatibility with their existing applications.
So, to avoid errors about "missing a switch" or "invalid switch" when passing filepaths 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 WinAPI can generally use them interchangeably. However, 3rd party applications will probably 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 /
.
In the context of C#: It should be noted, since this is technically a C# question, that if you want to write more "portable" C# code that works on both Unix and Windows (even if C# is predominantly a Windows language), you might want to use the Path.DirectorySeparatorChar
field so your code uses the preferred separator on that system, and use Path.Combine()
to append paths properly.
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#.
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.
\
is correct in a Windows file
path and /
is correct in a URI.This can be relevant resource.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With