文件路径中的和\有什么区别 [英] What is the difference between and \ in file path
问题描述
Windows等操作系统的文件路径中单斜线和双斜线有什么区别
What is the difference between single slash and double slash in file path for Windows operating system such as
c:\PersonalMyFolder\MyFile.jpg
和
c:PersonalMyFolderMyFile.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:PersonalMyFolderMyFile.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:PersonalMyFolderMyFile.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:PersonalMyFolderMyFile.jpg";
这篇关于文件路径中的和\有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!