get directory from full path

前端 未结 10 1423
慢半拍i
慢半拍i 2021-01-01 11:54

If i have:

C:\\temp\\foo\\bar\\

(NOTE: bar is a directory)

how can i parse out:

bar

10条回答
  •  梦谈多话
    2021-01-01 12:49

    I can think of 4 ways instantly

    1

    • If the string ends with a slash remove it
    • Use Path.GetFilename (or numerous other System.IO methods)

    2

    • Split the string on slashes into an array
    • Get the last index of the array

    3

    • Create a Uri class with it in the constructor
    • Use the Segments property

    4

    • The linq way someone mentioned above

提交回复
热议问题