Java 中是否有等效于 File.separator 的 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a C# equivalent to File.separator in Java
提问by peter.murray.rust
How does C# cope with file separators? Is it always assumed the file separator is "\" (or "\\" and does this have to be escaped like this?
C# 如何处理文件分隔符?是否总是假设文件分隔符是“\”(或“\\”,是否必须像这样转义?
采纳答案by Geo
I think Path.DirectorySeparatorChar
is what you need. You can find it in the System.IO
namespace.
我认为Path.DirectorySeparatorChar
这就是你所需要的。您可以在System.IO
命名空间中找到它。
回答by Yannick Motton
To combine paths use, and as suggested by Geo, this works in conjunction with DirectorySeparatorChar
要结合使用路径,并按照 Geo 的建议,这可以与 DirectorySeparatorChar
IO.Path.Combine("path", "file.ext");
回答by adrianbanks
If by file separators, you really mean directory separators, then you need Path.DirectorySeparatorCharand Path.AltDirectorySeparatorChar.
如果通过文件分隔符,你的意思是目录分隔符,那么你需要Path.DirectorySeparatorChar和Path.AltDirectorySeparatorChar。
These will get the relevant platform-specific characters, without having to hard-code them (and also avoiding the need to escape them properly).
这些将获得相关的特定于平台的字符,而无需对它们进行硬编码(也避免了正确转义它们的需要)。