使用/或\\在C#中的文件夹路径 [英] Using / or \\ for folder paths in C#

查看:759
本文介绍了使用/或\\在C#中的文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在C#编写的文件路径,我发现我可以写的东西,如C:\或C:/,并得到了相同的路径。哪一个建议?我听说的地方,使用的是单人/被推荐比使用更\(以\作为一个转义序列)。

When writing file paths in C#, I found that I can either write something like "C:\" or "C:/" and get the same path. Which one is recommended? I heard somewhere that using a single / was more recommended than using \ (with \ as an escaped sequence).

推荐答案

窗支持路径分隔符,所以两者将工作,至少在本地路径(/不会对网络路径工作)。问题是,没有在Windows上使用工作,但非标准路径分隔符(/)没有实际的好处,尤其是因为您可以使用逐字字符串:

Windows supports both path separators, so both will work, at least for local paths (/ won't work for network paths). The thing is that there is no actual benefit of using the working but non standard path separator (/) on Windows, especially because you can use the verbatim string literal:

string path = @"C:\"  //Look ma, no escape

在那里我可以看到使用/分离器的一个好处是,当你只有相对路径工作,将使用代码Windows和Linux的唯一情况。然后你就可以拥有../foo/bar/baz指向同一个目录下。但是,即使在这种情况下,最好是离开System.IO命名空间( Path.DirectorySeparatorChar Path.Combine )照顾等的问题。

The only case where I could see a benefit of using the / separator is when you'll work with relative paths only and will use the code in Windows and Linux. Then you can have "../foo/bar/baz" point to the same directory. But even in this case is better to leave the System.IO namespace (Path.DirectorySeparatorChar, Path.Combine) to take care of such issues.

这篇关于使用/或\\在C#中的文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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