path和\\在文件路径中有什么区别 [英] What is the difference between \ and \\ in file path

查看:362
本文介绍了path和\\在文件路径中有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows操作系统的文件路径中,单斜杠和双斜杠有什么区别?

What is the difference between single slash and double slash in file path for Windows operating system such as

c:\\Personal\MyFolder\\MyFile.jpg

c:\Personal\MyFolder\MyFile.jpg

如果我使用单斜线或双斜线,那是怎么回事,因为我已尝试将两个图像都存储在我的代码中(在webconfig文件中)并且它们都可以正常工作。

What if I use the single or double slash because I have tried both for storing images in my code (in webconfig file) and both of them work fine.

有什么区别吗?

推荐答案

Windows会忽略双反斜杠。因此,尽管使用 \ 的第二种语法是正确的,但您应该使用该语法,而使用 \\ 的第一种语法是正确的

Windows ignores double backslashes. So while the second syntax with \ is correct and you should use that one, the first with \\ works too.

唯一的例外是在表示UNC路径的路径的开始处使用双反斜杠。

请参见通用命名约定

The only exception is double-backslash at the very beginning of a path that indicates a UNC path.
See Universal Naming Convention.

尽管请注意,在许多编程语言(如C,C ++,Java,C#,Python,PHP,Perl)中,反斜杠用作转义符字符串中文字。因此,它需要自己进行转义(通常使用另一个反斜杠)。因此,在这些语言中,通常需要在字符串文字中使用双反斜杠以实际获取路径的单反斜杠。因此,例如在C#中,以下字符串文字实际上被解释为 C:\Personal\MyFolder\MyFile.jpg

Though note that in many programming languages like C, C++, Java, C#, Python, PHP, Perl, a backslash works as an escape character in string literals. As such, it needs to be escaped itself (usually with another backslash). So in these languages, you usually need to use a double backslash in the string literal to actually get a single backslash for a path. So for example in C#, the following string literal is actually interpreted as C:\Personal\MyFolder\MyFile.jpg:

var path = "C:\\Personal\\MyFolder\\MyFile.jpg";

尽管有替代语法。例如,在C#中,您可以使用以下语法获得相同的结果:

Though there are alternative syntaxes. For example in C#, you can use the following syntax with the same result:

var path = @"C:\Personal\MyFolder\MyFile.jpg";

这篇关于path和\\在文件路径中有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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