简体   繁体   English

Java中是否存在与File.separator等效的C#

[英]Is there a C# equivalent to File.separator in Java

How does C# cope with file separators? C#如何处理文件分隔符? Is it always assumed the file separator is "\\" (or "\\\\" and does this have to be escaped like this? 总是假设文件分隔符是“\\”(或“\\\\”,这是否必须像这样进行转义?

I think Path.DirectorySeparatorChar is what you need. 我认为Path.DirectorySeparatorChar就是你所需要的。 You can find it in the System.IO namespace. 您可以在System.IO命名空间中找到它。

要组合路径使用,并按照Geo的建议,它与DirectorySeparatorChar一起使用

IO.Path.Combine("path", "file.ext");

If by file separators, you really mean directory separators, then you need Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar . 如果通过文件分隔符,您实际上是指目录分隔符,那么您需要Path.DirectorySeparatorCharPath.AltDirectorySeparatorChar

These will get the relevant platform-specific characters, without having to hard-code them (and also avoiding the need to escape them properly). 这些将获得相关的平台特定字符,而无需对它们进行硬编码(并且还避免需要正确地转义它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM