简体   繁体   English

从路径中获取文件夹名称

[英]Get a folder name from a path

I have some path c:\\server\\folderName1\\another name\\something\\another folder\\ . 我有一些路径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: DirectoryInfo.Name有效:

using System;
using System.IO;

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

也可以使用System.IO.Path:

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

使用这一行System.Linq命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM