在文件路径斜杠(/)和反斜杠(\)之间的区别 [英] Difference between forward slash (/) and backslash (\) in file path

查看:2609
本文介绍了在文件路径斜杠(/)和反斜杠(\)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道\ 和 / 文件路径之间的差异。我注意到,有些时候路径中包含 / 和一些次 \

I was wondering about the difference between \ and / in file paths. I have noticed that some times path contains /and some times with \.

这将是巨大的,如果任何人都可以解释何时使用 \ /

It would be great if anyone can explain when to use \ and /.

推荐答案

/ 是Unix和类Unix系统路径分隔符。现代的Windows通常可以同时使用 \ / 互换的文件路径,但微软已经提倡使用<$的C $ C> \ 作为路径分隔了几十年。

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

这是该日期可追溯到20世纪70年代的历史原因做的,通过十多年的Windows日期之前。一开始,MS-DOS(基础,以早期Windows)不支持目录。 Unix的使用 / 字符从一开始有目录的支持。然而,当在MS-DOS 2.0中添加目录,微软和IBM已经使用 / 字符的命令开关的,而且由于DOS的轻量级解析器(从 QDOS下降,设计用于在低端硬件上运行),他们无法找到使用 / 字符不中断与现有应用程序的兼容性一条可行的途径。

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

没有错误。

后来,微软和IBM合作进行操作系统无关DOS称为 OS / 2 。 OS / 2必须同时使用分离器,可能吸引更多的Unix开发的能力。当微软和IBM于1990年分道扬镳,微软花了他们有什么样的代码创造的的Windows NT ,其上的Windows的所有现代版本的基础,背着它这个分隔不可知论。

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.

由于向后兼容性已经从所有,他们已经采取的(DOS下的Win16 / DOS主要的OS过渡一直是游戏为微软的名字,Win16的/ Win32中,Win32的/ WINNT),这种特性卡住了,而且它可能会存在一段时间呢。

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.

这是出于这个原因,这种差异的存在。这要真有你在做什么,因为,就像我说的,WinAPI的一般可以互换使用它们没有影响。但是,如果你通过第三方应用程序可能会打破 / 时,他们期待一个 \ 目录名称之间。如果你使用的是Windows,坚持使用 \ 。如果你正在使用Unix或 URI 秒(其中有他们在Unix中路径的基础上,但这是另一个故事了),那么使用 /

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

在C#中的上下文应该注意,因为这的的技术上是一个C#的问题,如果你想要写更多的便携式的C#代码在Unix和Windows(工程,即使C#主要是在Windows的语言),您可能需要使用 Path.DirectorySeparatorChar 字段,以便您的代码使用该系统上的首选分离器,并使用 Path.Combine ()来适当追加路径。

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.

这篇关于在文件路径斜杠(/)和反斜杠(\)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆