如何获得.NET的Path.Combine将正斜杠转换为反斜杠? [英] How do I get .NET's Path.Combine to convert forward slashes to backslashes?
问题描述
我正在像这样使用Path.Combine:
I'm using Path.Combine like so:
Path.Combine("test1/test2", "test3\\test4");
我得到的输出是:
test1/test2\test3\test4
请注意,正斜杠不会转换为反斜杠.我知道我可以做string.Replace以更改它,但是有更好的方法吗?
Notice how the forward slash doesn't get converted to a backslash. I know I can do string.Replace to change it, but is there a better way of doing this?
推荐答案
由于您的"test1/test2"已经是字符串文字,因此Path.Combine不会将您的"/"更改为"\".
Because your "test1/test2" is already a string literal, Path.Combine will not change the '/' for you to a '\'.
Path.Combine只会从输出中连接2个字符串文字和操作系统使用的适当路径定界符(在本例中为Windows,即\),
Path.Combine will only concat the 2 string literals with the appropriate path delimiter used by the OS, in this case Windows, which is '\', from there your output
test1/test2\test3\test4
您最好的选择是替换字符串.
Your best bet would be the string.Replace.
这篇关于如何获得.NET的Path.Combine将正斜杠转换为反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!