get directory from full path

前端 未结 10 1385
慢半拍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:44

    The simplest way to do this without creating a new DirectoryInfo instance is to use the Path.GetFileName static method. This is located in System.IO.

    using System.IO;
    
    string lastFolderName = Path.GetFileName(@"C:\Folder1\Folder2");
    

    The variable would be set to "Folder2".

    This is quite a bit more efficient that creating a new instance of the DirectoryInfo class!

提交回复
热议问题