I am using a relative file path in one of the cs file to get a location to save an image.
我在其中一个cs文件中使用相对文件路径来获取保存图像的位置。
Is there any difference in using ../ and ..// for getting the path.
使用../和..//获取路径有什么不同。
2 个解决方案
#1
I don't know if your slashes are actually backslashes, but in c#, you have to escape backslashes.
我不知道你的斜杠是否实际上是反斜杠,但在c#中,你必须逃避反斜杠。
var path = "..\\file.txt";
path's value is actually ..\file.txt, because the "\" is actually one (escaped) backslash.
path的值实际上是.. \ file.txt,因为“\”实际上是一个(转义)反斜杠。
However, if it is:
但是,如果是:
var path = @"..\file.txt";
then it is the same. The @ means you want the string as-is, without any escaping, so both "path" variables are the same.
那就是一样的。 @表示您希望字符串按原样,没有任何转义,因此两个“路径”变量都是相同的。
#2
On Unix, and I think MS-DOS and hence Windows follows Unix closely enough here that it is not a difference between the systems, then you can have any number of consecutive slashes at any point in a pathname and it is equivalent to a single slash. Consequently, your two examples are equivalent.
在Unix上,我认为MS-DOS和Windows因此非常接近Unix,它不是系统之间的差异,那么你可以在路径名中的任何一点有任意数量的连续斜杠,它相当于一个斜杠。因此,您的两个示例是等效的。
Note that on Windows, a double slash at the start of a path name indicates a UNC path - a machine name followed by a path on that machine.
请注意,在Windows上,路径名称开头的双斜杠表示UNC路径 - 机器名称后跟该机器上的路径。
#1
I don't know if your slashes are actually backslashes, but in c#, you have to escape backslashes.
我不知道你的斜杠是否实际上是反斜杠,但在c#中,你必须逃避反斜杠。
var path = "..\\file.txt";
path's value is actually ..\file.txt, because the "\" is actually one (escaped) backslash.
path的值实际上是.. \ file.txt,因为“\”实际上是一个(转义)反斜杠。
However, if it is:
但是,如果是:
var path = @"..\file.txt";
then it is the same. The @ means you want the string as-is, without any escaping, so both "path" variables are the same.
那就是一样的。 @表示您希望字符串按原样,没有任何转义,因此两个“路径”变量都是相同的。
#2
On Unix, and I think MS-DOS and hence Windows follows Unix closely enough here that it is not a difference between the systems, then you can have any number of consecutive slashes at any point in a pathname and it is equivalent to a single slash. Consequently, your two examples are equivalent.
在Unix上,我认为MS-DOS和Windows因此非常接近Unix,它不是系统之间的差异,那么你可以在路径名中的任何一点有任意数量的连续斜杠,它相当于一个斜杠。因此,您的两个示例是等效的。
Note that on Windows, a double slash at the start of a path name indicates a UNC path - a machine name followed by a path on that machine.
请注意,在Windows上,路径名称开头的双斜杠表示UNC路径 - 机器名称后跟该机器上的路径。