How do I get .NET's Path.Combine to convert forward slashes to backslashes?

前端 未结 8 2165
[愿得一人]
[愿得一人] 2021-02-03 16:39

I\'m using Path.Combine like so:

Path.Combine(\"test1/test2\", \"test3\\\\test4\");

The output I get is:

test1/test2\\test3\\te         


        
相关标签:
8条回答
  • 2021-02-03 17:34

    Try using

    var fullname = new DirectoryInfo(Path.Combine("f:/","test1/test2", "test3\\test4")).FullName;
    

    This will result in

    f:\test1\test2\test3\test4
    
    0 讨论(0)
  • 2021-02-03 17:39

    First, I would argue in this particular case, it wouldn't be unreasonable to do a single .Replace()

    Secondly, you could also use System.Uri to format your path, it's very strict. However, this will be more lines than a simple .Replace(). I apperently am voting for you to just use .Replace() be done with it! Hope that helps

    0 讨论(0)
提交回复
热议问题