【发布时间】:2011-01-25 08:38:43
【问题描述】:
我有一些路径c:\server\folderName1\another name\something\another folder\。
如何从那里提取最后一个文件夹名称?
我尝试了几件事,但都没有成功。
我只是不想搜索最后一个\,然后再拿其余的。
谢谢。
【问题讨论】:
标签: c#
我有一些路径c:\server\folderName1\another name\something\another folder\。
如何从那里提取最后一个文件夹名称?
我尝试了几件事,但都没有成功。
我只是不想搜索最后一个\,然后再拿其余的。
谢谢。
【问题讨论】:
标签: c#
string a = new System.IO.DirectoryInfo(@"c:\server\folderName1\another name\something\another folder\").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
}
}
【讨论】:
查看DirectoryInfo.Name。
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx
【讨论】:
也可以使用 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]
【讨论】: