What is the difference between \ and \\ in file path

六眼飞鱼酱① 提交于 2019-12-06 20:23:30

问题


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

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

and

c:\Personal\MyFolder\MyFile.jpg

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.

Is there any difference??


回答1:


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

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


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";

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";


来源:https://stackoverflow.com/questions/15969608/what-is-the-difference-between-and-in-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!