Path.Combine() does not add directory separator after drive letter

前端 未结 1 885
小蘑菇
小蘑菇 2021-01-17 15:23

Am I using the Path.Combine Method Incorrectly?
I Get This Result With Path.Combine(string[]):

C:Users\\\\Admin\\\\AppData\\\\R         


        
相关标签:
1条回答
  • 2021-01-17 15:28

    It's in the documentation:

    If path1 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character [..], DirectorySeparatorChar is appended to path1 before concatenation.

    So your first path, which is the drive letter, does not get a directory separator appended. You can for example fix this after splitting, by appending the separator to TempSave[0], but that'll cause issues with Linux like you said.

    You can also solve your actual problem differently, see How do I find the parent directory in C#?, or just append ..\..\ like @Dan suggests so you can skip the splitting:

    string desiredPath = Path.Combine(Application.UserAppDataPath, @"..\..\");
    
    0 讨论(0)
提交回复
热议问题