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

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

问题描述

我想知道 / 在文件路径上的区别.我注意到有时路径包含 / 有时它包含 .

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

推荐答案

/ 是 Unix 和类 Unix 系统上的路径分隔符.现代 Windows 通常可以将 / 交替用于文件路径,但微软几十年来一直提倡使用 作为路径分隔符.

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

这样做的历史原因可以追溯到 1970 年代,比 Windows 早十多年.一开始,MS-DOS(早期 Windows 的基础)不支持目录.Unix 从一开始就使用 / 字符支持目录.但是,当在 MS-DOS 2.0 中添加目录时,Microsoft 和 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 folder1folder2

没有错误.

后来,微软和 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、到 Win16/Win32、到 Win32/WinNT)中,向后兼容性一直是 Microsoft 游戏的名称,因此这种特性被卡住了,并且它可能还会存在一段时间.

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 通常可以互换使用它们.但是,如果在目录名称之间期望 时传递 / ,则第 3 方应用程序可能会中断.如果您使用的是 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天全站免登陆