开发者

Get a folder name from a path

开发者 https://www.devze.com 2022-12-22 03:37 出处:网络
I have some pathc:\\server\\folderName1\\another 开发者_运维百科name\\something\\another folder\\ .

I have some path c:\server\folderName1\another 开发者_运维百科name\something\another folder\ .

How i can extract from there the last folder name ?

I have tried several things but they didn't work.

I just don't want to search for the last \ and then to take the rest .

Thanks.


string a = new System.IO.DirectoryInfo(@"c:\server\folderName1\another name\something\another folder\").Name;


DirectoryInfo.Name works:

using System;
using System.IO;

class Test
{
    static void Main()
    {
        DirectoryInfo info = new DirectoryInfo("c:\\users\\jon\\test\\");
        Console.WriteLine(info.Name); // Prints test
    }                                                
}


Check out DirectoryInfo.Name.

http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx


Also possible using System.IO.Path:

string s = Path.GetFileName(Path.GetDirectoryName(@"c:\server\folderName1\another name\something\another folder\"));


use this one line System.Linq command:

foldername.Split(Path.DirectorySeparatorChar).Reverse().ToArray()[0]
0

精彩评论

暂无评论...
验证码 换一张
取 消