簡體   English   中英

從路徑中獲取文件夾名稱

[英]Get a folder name from a path

我有一些路徑c:\\server\\folderName1\\another name\\something\\another folder\\

我怎么能從那里提取最后一個文件夾名稱?

我嘗試了幾件事,但他們沒有用。

我只是不想搜索最后一個\\然后接下來。

謝謝。

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

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